URL re-writing using .htaccess - .htaccess

I have a URL:
domain.com/abc/hotel_detail.php?id=2
And I want to do a URL re-write to make it look like this :
domain.com/abc/hotel_detail/2
My current .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^id=20$
RewriteRule ^abc/hotel_detail/([0-9]+)$ ^abc/hotel_detail.php?id=$1 [L]
The first part contains code for removing the extension(.php) from pages.
When I try to open this link domain.com/abc/hotel_detail/2 , it gives me object not found error.
Can Someone please tell me whats wrong with the code?

Remove this line:
RewriteCond %{QUERY_STRING} ^id=20$

Changing your file to:
RewriteEngine On
RewriteRule ^abc/hotel_detail/([0-9]+)$ ^abc/hotel_detail.php?id=$1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,R]
should work. I removed RewriteCond %{QUERY_STRING} ^id=20
Your first rewritecond and rule was overwriting the second one, switching them around should fix it.

I think this should work
RewriteRule ^abc/hotel_detail/([0-9]*)$ abc/hotel_detail.php?id=$1 [L]
([0-9]*) miltiply sign.
and the ^ for the real link

Related

GET parameter doesn't work with htaccess rules

So, I have this code in .htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Every file loses its extension (/index.php -> /index etc) and that works perfectly fine. But later on, I started working with admin panel and I'm using some GET parameters there. For example, problematic URL look like:
example.com/admin?cat=1
As far as I know, RewriteRule gets only string after RewriteBase and is not catching GET parameters, right? So why when I try to go to this URL it rewrites it to this?
http://example.com/C:/OpenServer/domains/example.com/admin/1/
There is also this line in .htaccess(but doesn't look like there is problem with it):
RewriteRule ^admin/(.*)$ admin.php?cat=$1
As answered here, you have to add [QSA] at the end of your RewriteRule line:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA]
Google search query that lead me to that answer: htaccess pass parameters

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.

.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]

mywebsite.com/project.php?id=123 should redirect via .htaccess to mywebsite.com/123

mywebsite.com/123 should display content via .htaccess of mywebsite.com/project.php?id=123 - unfortunately I changed the .htaccess rewriterule yesterday for anothe reason and now the redirection does not function (it goes to our 404-error page). Can you see what I've done wrong here? Many thanks!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9\-_]+)/?$ $1.php [NC,L]
Changing the last line to this should work:
RewriteRule ^([a-z0-9\-_]+)/?$ project.php?id=$1 [NC,QSA,L]
if you want the regex rule not to catch URLs that contain alphabetic characters, use
RewriteRule ^([0-9\-_]+)/?$ project.php?id=$1 [NC,QSA,L]
And for your new requirement:
RewriteRule ^([0-9\-_]+)/?$ project.php?id=$1 [NC,QSA,L]
RewriteRule ^([a-z\-_]+)/?$ new.php?id=$1 [NC,QSA,L]

removing two GET parameters from url using .htaccess

I have a url that looks like this
http://example.com/index.php?con=something&met=meh
What i'm trying to do is get rid of con= and met= so the url would look like
http://example.com/index.php/something/meh
That's what i've done so far
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ action=$1 [L,QSA]
RewriteRule ^(.*)$ page=$1 [L,QSA]
but nothing changes, the url it still look the same http://example.com/index.php?con=something&met=meh
What am i doing wrong?
I did like this:
RewriteEngine on
RewriteRule ^index.php/([^/]+)/?([^/]*) /index.php?con=$1&meh=$2 [NC]
Notice that if you don't pass any meh, it still works.

Resources