mod_rewrite not working - .htaccess

I'm new to working with .htaccess and I'm having trouble using mod_rewrite for apache.
So basically I have a URL. www.website.com/test/index.php
and I'd like it to be shorter www.website.com/t/index.php
So in my .htaccess file I have these three lines:
RewriteEngine On
RewriteBase /
RewriteRule ^test/(.*) t/$1 [R=301,L]
This works perfectly for directing me to www.website.com/t/index.php but the index.php page wont load and will only display a 404 error. Any help would be greatly apprectiated.

You need to do the shortening of your URL first:
RewriteEngine On
RewriteRule ^test/(.*) t/$1 [R=301,L]
...and then make sure, the new (and nonexisting) URL will get processed:
RewriteRule ^t/(.*) test/$1 [L]
The first rewrite is "external", so it is displayed in client browser address bar, but the second is "internal", so apache only displays results from the new URL in place of the old one w/o any change in clients address bar.

Related

Rewrite url via htaccess on apache server

I have a url which is
www.domain.com/index.php?route=ticketsystem/generatetickets
I want people who type in the url www.domain.com/contact to be redirected to the page index.php?route=ticketsystem/generatetickets however have the url bar still show /contact is that possible via htaccess and if so how? I tried the below
RewriteEngine On
RewriteBase /
RewriteRule contact /index.php?route=ticketsystem/generatetickets [L,QSA]
For your shown attempts, please try following .htaccess rules file. Make sure your index.php and .htaccess files are present in same directory/folder. Also better to use & rather than using / for query string.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^contact/?$ index.php?route=ticketsystem&generatetickets [QSA,NC,L]

How to rewrite url in htaccess without redirecting on localhost?

Trying to ask my question again and explain it the best I can.
I am running WAMP on windows 10. Apache version 2.4.18. My project is in www folder named hoidja.ee. mod_rewrite is enabled, AllowOverrideis set to All.
Inside that folder I have .htaccess file, where I am trying to accomplish rewrite without redirect and it does not work at all. I know the .htaccess file is working, since I can rewrite index.php to /home/ for example and it is working perfectly fine.
I have a page with all the users listing. You can click on the user, and it will take you to a url:
http://localhost/Hoidja.ee/hoidja.php?user_id=94&username=John
How I would like this to show is
http://localhost/Hoidja.ee/user/John
without any redirection.
What I have tried:
#tried first with only user_id
RewriteCond %{QUERY_STRING} ^user_id=([A-Za-z0-9-]+)$
RewriteRule ^hoidja\.php$ /user/%1? [L]
This gave me 404 that /user/94 doesn't exist.
RewriteRule ^user/([0-9]+)/([A-Za-z0-9-]+)/?$ hoidja.php?user_id=$1&username=$2 [QSA]
This doesn't do anything visually. If I enter http://localhost/Hoidja.ee/user/94/John in the url manually, it goes to that page and shows the user data, but if I enter the original url it does not rewrite it. I am using <base> as well.
I have tried all the possible ways but nothing seems to work. How can I accomplish the url rewrite?
You are getting the 404 error because you are rewriting to a non-existent path /user/94 .you will need to redirect /hoidja.php?user_id= to /user/94 and then rewrite /user/94 back to the real location.
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^user_id=([A-Za-z0-9-]+)$
RewriteRule hoidja\.php$ /hoidja.ee/user/%1? [L,R]
RewriteRule ^(?:hoidja\.ee/)?user/(.+)$ /hoidja.ee/hoidja.php?user_id=$1 [L]

htaccess: how to get cpanel username out of url and redirect

I have the following htaccess lines...
RewriteRule ^([a-zA-Z0-9]+)?$ index.php?p1=$1 [L]
RewriteRule ^~([a-zA-Z0-9]+)/([a-zA-Z0-9]+)?$ ~$1/index.php?p1=$2 [L]
The first line works fine, its the second line that doesn't work at all...
The first line does this....
domain.com/about -> domain.com/index.php?p1=about
What I'm trying to do with the second line...
if the url is server1.domain.com/~username/about....
I need it to translate to server1.domain.com/~username/index.php?p1=about
Basically, detecting if there is a ~
I am trying to work out my code to allow for the development url of the hostname/~username
Right now it is showing the green apache 404 not found page when trying to visit the website using that code.
Please let me know if you need any more information
Switch the order of the rules and add a RewriteBase:
RewriteBase /
RewriteRule ^~([a-zA-Z0-9]+)/([a-zA-Z0-9]+)?$ ~$1/index.php?p1=$2 [L]
RewriteRule ^([a-zA-Z0-9]+)?$ index.php?p1=$1 [L]
You need to be sure that this .htaccess file is triggered both on requests to pages from domain.com and pages from server1.domain.com.
Also, if you have a .htaccess file in any subdirectories (you shouldn't, based on your problem description), you will have to modify those accordingly, but we would need more information.
You need to place this rule in your httpd.conf:
RewriteEngine On
RewriteRule ^/?(~[a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ /$1/index.php?p1=$2 [L,QSA]
And make sure this line is uncommented in httpd.conf:
LoadModule userdir_module modules/mod_userdir.so
Then make sure index.php is present directly under ~username/

How to stop htaccess redirect replacing URL

I have the following in my .htaccess file which works fine, except that when I hit:
http://bunkerbuster.com/profiles/1
The URL in the address bar is replaced with the new URL. I don't want this to happen, I want the URL to still show bunkerbuster.com
Thanks for your help.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^profiles/([0-9]+)$ http://projectzebra-bunkerbuster.herokuapp.com/profiles/$1 [R,L]
RewriteRule ^profiles/([0-9]+)/$ http://projectzebra-bunkerbuster.herokuapp.com/profiles/$1 [R,L]
What you seem to be trying is make Apache act as a proxy.
This can't be done using htaccess but can be done using mod_proxy :
http://httpd.apache.org/docs/2.0/mod/mod_proxy.html#proxypass

htaccess URL Rewrite for products doesn't redirect me

There is plenty of information out there but nothing I've read on the interwebz has given me an answer as to why my htaccess is not working.
I cannot determine why my rule isn't rewriting the URL as I thought it would. I have the following url:
domain.com/Book/bookpage/index.php?bookID=123&bookName=foo_bar
I would like to change it so that when someone hits that URL, it shows like:
domain.com/Book/123/foo_bar
I started off trying to get it to work using just the Book ID and haven't even gotten that to work.
This is what I have thus far:
RewriteRule ^Book/([0-9]+)$ /Book/bookpage/index.php?bookID=$2
However, after placing that htaccess in the root of the site and going to the URL:
domain.com/Book/bookpage/index.php?bookID=123
The URL in the address bar remains the same.
try this:
RewriteEngine On
RewriteBase /
# this rewrite domain.com/Book/123 or domain.com/Book/123/
RewriteRule ^Book/([0-9]+)/?$ /Book/bookpage/index.php?bookID=$2 [L,NC,QSA]
# this rewrite domain.com/Book/123/title or domain.com/Book/123/title/
RewriteRule ^Book/([0-9]+)/([a-z0-9\-_]+)/?$ /Book/bookpage/index.php?bookID=$1&bookName=$2 [L,NC,QSA]
Try adding [L,R=301] at the end of the line:
RewriteRule ^Book/([0-9]+)$ /Book/bookpage/index.php?bookID=$2 [L,R=301]

Resources