Pretty URL htaccess - php

I have the below url that I want to prettify. Any help appreciated.
From;
http://www.example.co.uk/search-menu?r_id=1&name=testname
To
http://www.abklab.co.uk/search-menu/1/testname
I have tried below but getting Internal Server Error
# external redirect from actual URL to pretty one (WEB)
RewriteCond %{THE_REQUEST} \s/+search-menu(?:\.php)?\?r_id=([^\s&]+)&name=([^\s&]+) [NC]
RewriteRule ^ /search-menu/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one (WEB)
RewriteRule ^search-menu/([\w-]+)/([\w-]+)/?$ search-menu.php?r_id=$1&name=%2 [L,QSA,NC]
FYI: Below rule removes .php extension site wide;
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

Try this in your htaccess :
RewriteEngine on
RewriteCond %{THE_REQUEST} /search-menu\?r_id=([^&]+)&name=([^\s]+) [NC]
RewriteRule ^ %1/%2? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search-menu/([^/]+)/([^/]+)/?$ search-menu.php?r_id=$1&name=$2 [NC,L]

Found something for you - the wizard will help you make your URL perfect
http://www.generateit.net/mod-rewrite/index.php

Add this line to your .htaccess file. It will take the variables and make is nice and pretty:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ /search-menu?r_id=$1&name=$2 [L]
Keep in mind this is basic and you will need to modify it to your addresses but it is just to give you the idea of what you need to do.

Related

Pretty URL .htaccess gives 404 not found

I'm really not that good at writing htaccess. It looks like gibberish for me. :-)
So what we wan't to do, is to remove the .php extension to the URL.
Here we have our script
RewriteEngine On
RewriteBase /
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
RewriteRule ^help/([A-Za-z0-9_]+)/?$ help.php?artid=$1 [NC,L] # Process parrots
RewriteRule ^signature/([A-Za-z0-9_]+)/?$ signature.php?artid=$1 [NC,L] # Process parrots
RewriteRule ^signatures/([A-Za-z0-9_]+)/?$ signatures.php?artid=$1 [NC,L] # Process parrots
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
I really cant find the issue here.
The issue is, that when i f.x goto localhost/secure/profil it gives me a 404 not found. But when i comment ALL the .htaccess out, and goto localhost/secure/profil.php it works.
The exact url when htaccess is turned on looks like this:
http://localhost/secure/profil/
Maybe somebody can see what i'm doing wrong here?
Any help would be appreciated!
Kind Regards!

convert dynamic url to seo friendly url in htaccess

Cuurent URL is
https://www.website.com/colleges.php?id=NR%20School%20of%20Architecture
we want to change this URL into SEO friendly URL as like,
https://www.website.com/colleges/NR-School-of-Architecture
we used this htaccess,but only the spaces were removed
RewriteEngine On
RewriteBase /
# replace all space by hyphen
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ %1-%2 [L,NE,R=302]
we used this htaccess to remove .php, it worked and .php was removed in the entire site.
But we dint get a seo freindly url like this,
colleges/NR-School-of-Architecture
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
kindly help us in achieving seo friendly url using htaccess
Thanks in advance.
You will need to define the rewrite rules in a .htaccess file placed in your site document root location after you have confirmed that mod-rewrite is enabled
RewriteEngine On
RewriteBase /
RewriteRule ^home /index.php

Rewriting "pretty links" causng 500 Internal Server Error

After much research, I've finally learned that mod_rewrite does not create "pretty links", rather it rewrites them to "normal" links that get processed by the server.
With some generous help here on SO, I have been able to do some basic URL rewriting, specifically removing the www. and also the .php file extension even when a user types in page.php. The .htaccess code for reference:
RewriteEngine On
# omit www.
RewriteCond %{HTTP_HOST} ^www\.website\.com [NC]
RewriteRule ^(.*)$ http://website.com/$1 [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
# To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
This website I'm working on has a news section, the links use a query string to pass the article id from links in the sidebar like so:
<a href="news.php?id=' . $phpVariable . '...etc.
After finding this amazing post HERE I began experimenting with this line of code from the post:
RewriteRule ^blog/([0-9]+)/([A-Za-z0-9-\+]+)/?$ /blog/index.php?id=$1&title=$2 [NC,L,QSA]
and modified it to match my website like so:
RewriteRule ^news/([0-9]+)$ news?id=$1 [NC,L,QSA]
Then I modified the link code in my news section to this:
<a href="news/' . $phpVariable . '/...etc.
so that the link would be "pretty" and be rewritten with the modified RewriteRule shown above.
The new RewriteRule was placed at the bottom of the .htaccess code (which may be part of the problem?) shown here:
RewriteEngine On
# omit www.
RewriteCond %{HTTP_HOST} ^www\.website\.com [NC]
RewriteRule ^(.*)$ http://website.com/$1 [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
# To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# rewrite pretty link to fetch news article
RewriteRule ^news/([0-9]+)$ news?id=$1 [NC,L,QSA]
Now when the news section is tested, any item selected from the sidebar causes a 500 Internal Server Error.
I'm just a bit in over my head, though thanks to the information here on SO, the regex is beginning to make more and more sense to me.
Based on question and comments I will suggest you to have it this way:
ErrorDocument 404 default
ErrorDocument 500 default
Options +FollowSymLinks -MultiViews
RewriteEngine On
# omit www.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# rewrite pretty link to fetch news article
RewriteRule ^news/(\d+)/?$ news.php?id=$1 [NC,L,QSA]
# To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]

.htaccsess rewrite rule for only one page

I am not familiar with the rewrite rules..
I am trying to get
export.php?type=pdf
to be able to be
export.php/pdf
I already have rewrite rules to remove .php from files..
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
so really it would be.
export?type=pdf
to
export/pdf
The issue I'm having is how do i go about doing this for JUST the export.php file ? I know its using Reg-ex for the rules, but i don't know enough to make a new rule, i have tried to Google it and find an answer on how to do it, but the issue is I'm not sure what this is called other than ".htaccess rewrite rule"
Try below rule after all your rules,
# incoming url is neither a file nor a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.+)/?$ $1.php?type=$2 [L]

hide.php files with variables htaccess

i have a problem on my htaccess file , i'm trying to hide all .php extensions on my website,
i tried many htaccess codes but most of it gives me errors , 404 Not Found , most of my files contain variables like ?id and ?category , index/?cat=Action , i want htaccess to hide php extension and forward /dir/foo to /dir/foo.php , help me pls :)
RewriteEngine On
RewriteBase /
# turn off index.php for home page
RewriteRule ^index\.php/?$ / [L,R=301,NC]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
You need to write php to the end of urls not the other way around. You can write variables like this: href="foo?bar=1". This code has worked for me:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
You can use this code in your root .htaccess:
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

Categories