I am trying to do a simple URL rewrite and I have searched apache.org and SO for an answer but have come up short. I executed the request and watched with a network utility and the server is throwing a 404. I am really sorry to be posting this simple question but I really don't see why this rule is not working. Thanks
I want to have this SEO-friendly url format
/products/my-product
Go to this physical file in the file system
/my-product.php
BTW: The folder "products" does not exist; it's just there for SEO reasons
Here is my .htaccess (Removing "RewriteBase /") has no effect
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
RewriteRule ^products/([A-Za-z\-]+)/?$ $1.php [L,QSA,NC]
Related
I want to be able to open the following url
http://example.com/login/12345
and in the background, it should load:
http://example.com/index.php?endpoint=login&access_key=12345
Obviously, I have tried many htaccess generators to create an appropriate RewriteRule set.
For example the following (from: http://www.generateit.net/mod-rewrite/index.php):
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?endpoint=$1&access_key=$2 [L]
Even though I know for a fact that .htaccess and mod_rewrite is enabled on my server (i tried to setup a test to redirect all requests from example.com to example2.com using htaccess and mod_rewrite, and it worked), I can't get this to work.
I have now spent nearly 2 hours to find for a solution on stackoverflow and other websites, but all my attempts failed.
I hope someone can help me find out the correct way to rewrite my url in the above example.like explained above.
Thanks a lot in advance!
Try with this:
RewriteEngine On
RewriteRule ^([^\/]*)/([^\/]*)$ /index.php?endpoint=$1&access_key=$2 [L]
#-Added-these---^--------^
When tested, here is the result:
Add this to the .htaccess file in your DOCUMENT_ROOT
Assuming login is not variable
RewriteEngine On
RewriteRule ^login/(\d+)$ index.php?endpoint=login&access_key=$1 [L,NC,DPI]
** If login is variable**
This is in case you want to redirect not only for login, but also for urls such as http://example.com/housenumber/12345
RewriteEngine On
RewriteRule ^([^/]+)/(\d+)$ index.php?endpoint=$1&access_key=$2 [L,NC,DPI]
Tested on actual Apache 2.2 and 2.4 :)
I want to show my 404 error page when ever someone gets on a non working / non created page. After a long time of searching on the web I couldnt find any solution so I post one myself. I dont have any .htaccess file either i created it filled it in but in my FTP it kept saying nothing was safed in there and it didnt show any bytes so thats something im also worried of.
So how can i get my .htaccess file to work and show my 404 Error page template? I followed all the steps to make a 404.php file but i dont mind it to be redirected to the same template I made for it but then as a page any of those solutions is fine.
You've got some rewrite rules in your .htaccess file that are preventing a 404 error from every truly happening...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
That basically states that if the requested resource is neither a file nor a directory (basically a 404 error), redirect to /index.php
Try this in your .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# delete the line below to have /index.php/anything trigger a 404
RewriteRule ^index\.php$ - [L]
</IfModule>
# END WordPress
#404 redirect
ErrorDocument 404 /httpdocs/wp-content/themes/avian/404.php
Note
This may make Wordpress quite unhappy however, I'm not sure how it uses internal routing, it may rely on breaking 404s to determine which pages to load by using fake (RESTful) URLs - reinstating 404 errors make cause Wordpress to stop working properly ... I am, however, no expert on it having barely ever touched it.
I'm trying to rewrite my url, but I'm getting an error "500 Internal Server Error"
I've never done this before, and I've read some tutorials but I can't say I got any smarter from it.
The .htaccess file is in the root directory (/var/www)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /sub_domain/directory_to_my_files/
RewriteRule ^([0-9.]+)-([0-9.]+)/(.*)/$ /index.php?pos=$1:$2&text=$3
The current link goes like this:
http://sub_domain.my_domain.com/directory_to_my_files/index.php?pos=(float|integer-only):(float|integer-only)&text=(any-text)
But I'm trying to do this:
http://sub_domain.my_domain.com/directory_to_my_files/(float|integer-only):(float|integer-only)/(any-text)/
Sorry if the links is a bit hard to read.
This should be placed in /directory_to_my_files/.htaccess
RewriteEngine On
RewriteBase /directory_to_my_files/
RewriteRule ^(-?[0-9.]+)[:-]([0-9.]+)/([^/]+)/?$ index.php?pos=$1:$2&text=$3 [L,QSA,NE]
I've tested this locally:
( I did not need the rewrite base )
RewriteEngine on
RewriteRule ^([0-9.]+)-([0-9.]+)/(.*) /index.php?pos=$1:$2&text=$3 [R,L]
And it has redirected me
from
http://loc.domain/555-666/misc-text
to
http://loc.domain/index.php?pos=555:666&text=misc-text
I reckon this was what you wanted?
About the [R,L] at the end:
the R is telling apache to actually redirect, not just rewrite the url - this helps with testing.
the L is saying this is the last rule
Edit: could it be that the rewriteBase is causing you problems? Try it without and see if it works - at least you can nail where the problem is, then.
I know there are TONS of questions on this website regarding this topic, as well as TONS of tutorials around google. However I have searched for a while now and I cannot seem to find one that explains my specific situation.
I am building a social application in which the whole application is being stored in a directory of a subdomain the website like so: subdomain.example.com/network/ <-- network being the directory of all the application files.
I have my .htaccess file located in the root of the subdomain and as of right now I have it removing .php from all the files and that is working throughout the entire project as it should. I have a user.php page which each user's profile is based off and the unique identifier for each user is their username (not 'id' like most people use.) So right now the urls look like /network/user.php?username=TylerB which is not very pretty. I have tried many different things to get this to look like /network/user/TylerB but nothing I try seems to work. I don't know if it's because it's in a directory, a subdomain or what. As of right now, when I have /network/user/TylerB (TylerB is my username) it gives me a 404 error.
Here is my current .htaccess file:
RewriteEngine On
RewriteRule ^user/([^/\.]+)/?$ user.php?username=$1 [L]
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
I would recommend setting the RewriteBase directive and matching on the full pattern, making sure to rewrite to the /network/ directory.
RewriteEngine On
RewriteBase /
RewriteRule ^network/user/([^/\.]+)/? /network/user.php?username=$1 [L]
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
Using the testing tool at http://htaccess.madewithlove.be/ I get an output of:
This rule was met, the new url is http://example.com/network/user.php?username=name
The tests are stopped because the L in your RewriteRule options
I have a custom CMS. It works very well except for one thing. I am wondering how best to implement 404 functionality. The site should only have the following url types
index.php?upn=something
or
gallery.php?groupId=2
Any directories that are appended to the url after index.php or gallery.php should throw 404's. Also I would like to throw a 404 if the content is not found in the database when a upn is specified. I realise that I can throw the header in php. But I said I would try to get a little more insight into this problem overall. So if ye have any info or pointers I'd appreciate them.
I like to do this programatically from my PHP code. You could write some simple Route Component, that try to find what you want. Today you're routing just to actions (index.php?upn= and gallery.php?groupId=) but someday in the future you may add new one, and you should change your .htaccess, which is more difficult. If you do it all in your app tier you can change it simpler.
Paste this code in a .htaccess file at the root of your project, and will route every request to router.php. Once you have the request you can decide what action to do.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ router.php?url=$1 [QSA,L]
</IfModule>
Hope it helps.
You'd have to return the 404 in the php code, but you can set the 404 handler like so in htaccess:
ErrorDocument 404 /errors/notfound.html