Inderjeet
Inderjeet

Reputation: 1528

hteaccess: Pretty url by htaccess showing 404 error

I am beginner in codding. I want make url pretty. I search this question on google then i found i have to use .htaccess. I tried htaccess code.

Here is my URL looklike website.com/project-details.php?Residential/The-Serenas/1

I want to make url like website.com/Residential/The-Serenas/1

I want to remove entire "project-details.php?" word from url by htaccess.

here is my .htaccess code

Options +FollowSymLinks

RewriteEngine On 

RewriteBase /basename/ 





# external redirect from actual URL to pretty one 

RewriteCond %{THE_REQUEST} /project-details\.php\?([^\s&]+) [NC] 

RewriteRule ^ view/%1? [R=302,L] 



RewriteCond %{THE_REQUEST} /project-details\?([^\s&]+) [NC] 

RewriteRule ^ view/%1? [R=302,L] 







# internally rewrites /view/ABC123 to project-details.php?ABC123 

RewriteRule ^view/([A-Za-z0-9_@./#&+-]+)$ project-details.php?$1 [L,NC,QSA] 





# PHP hiding rule 

RewriteCond %{REQUEST_FILENAME} !-d 

RewriteCond %{REQUEST_FILENAME} !-f 

RewriteCond %{REQUEST_FILENAME}.php -f 

RewriteRule ^(.*)$ $1.php [L] 

Above htaccess code work and url replacce to website.com/view/Residential/The-Serenas/1

If i remove view from this line RewriteRule ^ view/%1? [R=302,L].

Then page goes to 404 error. If i dont remove view then code worked but view show on url. I dont want to show anything on url only remove "project-details.php?" this.

Upvotes: 0

Views: 318

Answers (1)

Ben
Ben

Reputation: 5129

This rule is to check the request uri whether is /project-details.php and any query string follow behind, and redirect user to the uri that matches the query string. %1 is the backreference and provide access to the grouped parts (in parentheses) of the pattern, in this case ([^\s&]+). For example, if uri /project-details.php?Residential/The-Serenas/1 is requested, the user will be redirected to /Residential/The-Serenas/1.

RewriteCond %{THE_REQUEST} /project-details\.php\?([^\s&]+) [NC] 
RewriteRule ^ %1? [R=302,L] 

This rule is to check if user requests existing file and directory, if not, then the uri is rewritten internally (only server knows the destination uri, user does not know this). For example, if uri /Residential/The-Serenas/1 is requested, the server will rewrite the uri to project-details.php?/Residential/The-Serenas/1.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9_@./#&+-]+)$ project-details.php?$1 [L,NC,QSA] 

Upvotes: 1

Related Questions