Htaccess resposable links - .htaccess

I have link like this for example http://localhost/somepage.php?id=1. And i want to make link like this http://localhost/somepage/1 but not just on somepage.php but for all pages on localhost. Here is my code for hidding .php extension and now i now i got http://localhost/somepage?id=1 and now i need to get http://localhost/somepage/1.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

You can replace your rule with this rule:
RewriteEngine On
RewriteRule ^([^/]+)/(\d+)/?$ $1.php?id=$1 [QSA,L]

Related

Rewrite url for seo friendly htaccess

I want to rewrite this in SEO friendly url.
From - www.example.com/section.php?name=website
To - www.example.com/section/website
I tried this:
RewriteEngine On
RewriteRule ([^/\.]+)/([^/\.]+)?$ section.php?type=$1&service=$2 [NC,L]
I am fetching data using GET method in section.php and pass to the another page but after opening sub folder like www.example.com/{foldername} it returns to the section.php
Thanks. I will appreciate your help.
With your shown samples, please try following htaccess Rules file. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for rewrite to section.php as per OP's request.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ section.php?name=$1 [L]
This helps me
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ([^/\.]+)/([^/\.]+)/?$ search.php?type=$1&service=$2

Rewrite my URL to remove strings using htaccess

I need help with my htaccess file. I added the following to remove all .php extensions which is working fine.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
However I have a directory /users which I want to rewrite the URL strings. The current URL is like this:
aero.site/maki/users/index.php?t=mf5cc
I want the URL to look like
aero.site/maki/mf5cc
I used the following rewrite rule but it's giving me Object not found error:
RewriteRule ^([^/.]+)$ users/index.php?t=$1 [L]
My complete htaccess content is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^/.]+)$ users/index.php?t=$1 [L]
Kindly help me with the correct rewrite rule for the last line to turn aero.site/maki/users/index.php?t=mf5cc into aero.site/maki/mf5cc
FYI: my htaccess file is in aero.site/maki directory
I finally got it to work. Thanks to #misorude. Here's my updated .htaccess content:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^/.]+)$ users/index?t=$1 [L]

Htaccess Rewrite rule for removing a dot

Thanks for your help. I have been trying to solve this for an hour now.
I had this Htaccess that would rewrite /file.html to a script but decided to remove the extension and it turned out like this:
RewriteEngine On
#RewriteRule ^([^/]*)\.html$ /?file=$1 [L]
RewriteRule ^([^\.]+)$ /index.php?file=$1 [NC,L]
However, now I need it to work also with filename containing the dot (i.e. /file.with.dots) but that rule doesn't allow it.
This one works for files with dots but ending in .html
#RewriteRule ^(.*)\.html$ /index.php?file=$1 [NC]
This gets me an Internal Server Error
RewriteRule ^([^/]+)$ /index.php?file=$1 [NC,L]
You could just do this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/?$ /index.php?file=$1 [NC,L]
Let me know the result.

Is there a way to setup a dynamic htaccess rewrite

I'm need some help with an htaccess rewrite. I would like to do a clean url setup that is dynamic.
For example
mydomain.com/pagename/var1/var2/
mydomain.com/pagename2/var1/var2/var3/etc
mydomain.com/pagename3/var1/var2/var3/etc
I would like the first level to be alway be the filename in the rewrite?
mydomain.com/pagename.php?v1=var1&v2=var2&v3=var3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$0\.php -f [NC]
RewriteRule ^([^.]+)/?$ $0.php?v1=$1&v2=$2&v3=$v3 [L]
This will do your job done,
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^home/{0,1}$ pagename.php?v1=1[QSA,L]
RewriteRule ^sone/{0,1}$ pagename.php?v1=2[QSA,L]
RewriteRule ^music/{0,1}$ pagename.php?v1=3[QSA,L]
RewriteRule ^video/{0,1}$ pagename.php?v1=3[QSA,L]
</IfModule>
and the link look like that: mydomain.com/home but the real link is mydomain.com/pagename.php?v1=1

.htaccess hide filename at the end of the url

I am using the following code to rewrite a url on my website:
RewriteEngine On
RewriteBase /
RewriteRule ^about/organisation$ pages/about/organisation [NC,L]
This hides the "pages" bit of the url, But if the user types in "about/organisation/index.php" they still get to the page and index.php is shown. What I want to do is if they type in the index.php it redirects them to "about/organisation" effectively hiding the file name.
I have tried this but it didnt work.
RewriteRule ^about/organisation$ pages/about/organisation/index.php [NC,L]
What am I doing wrong?
EDIT: from the answers it seems that my server doesnt let me access the relevant files so Ill have to solve that one first
You can use:
RewriteEngine On
RewriteBase /
RewriteRule ^(about/organisation/).+$ $1 [R=302,L,NE,NC]
RewriteRule ^about/organisation/?$ pages/about/organisation [NC,L]
Maybe try your rule like this.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /about/organisation/([^/]+)
RewriteRule ^ /about/organisation? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
REwriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^about/organisation/?$ pages/about/organisation [NC,L]

Resources