htaccess RewriteRule redirects to index.php - php

I am really puzzeld, I want to use htaccess Rewrite to rewrite an url with parameter to one without.
This is my .htaccess file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^sitemap.xml$ sitemap.php [NC] # Handle requests for "sitemap"
RewriteRule ^category/([A-Za-z0-9-]+)/$ displaycategory.php?category_name=$1 [NC] # Handle requests for "categories"
RewriteRule ^items/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ item.php?category_name=$1&id=$2 [NC] # Handle requests for "single item"
Rule 1 and rule 3 are working fine, but the second url redirects to index.php
If I call this url:
http://example.de/category/test/
it redirects to
http://example.de/category/test/index.php

Your .htaccess rewrite rule is correct, and I assume its not this causing the problem. If you have a directory called test or another file very close to test Apache Multiviews could be the problem. Have you access to server logs? I bet there is something in the server config preventing the rule from working correctly.

Related

.htaccess file is not working in my Server

My site is located in https://itjmovies.com/milan/public/ and I want to rewrite the URL by .htaccess file. From https://itjmovies.com/milan/public/ To https://itjmovies.com/milan/ but it is not working.
And also https://itjmovies.com/milan/public/auth/index.php?page=new To https://itjmovies.com/milan/public/auth/new/ but this is also not working.
I have kept my .httaccess file in /www/wwwroot/itjmovies.com/milan/.htaccess
My .htaccess file:
RewriteEngine On
RewriteRule ^/(.*)$ /public/$1 [R=301,NC,L]
RewriteRule ^public/auth/([a-zA-Z]+) /index.php?page=$1 [QSA,L]
Thank You :)
I'm not sure that your edit made your question any clearer. In your question description the stated rewrites (from/to) appear to be the wrong way round to what I think they are intended (and conflict with the order in which you have written the directives), but anyway...
My assumptions:
/public should not be part of the visible URL. Although your site is located in the /milan/public directory. You are making requests of the form /milan/<anything>.
You need to internally rewrite all requests from /milan/<anything> /to /milan/public/<anything>.
Requests of the form /milan/public/auth/<something>/ (note the trailing slash, as stated in your example) should be internally rewritten to /milan/public/auth/index.php?page=<something>
I would have 2 .htaccess files. One in the /milan subdirectory that simply rewrites/forwards requests to the public subdirectory. And another .htaccess file in /milan/public that handles rewrites that are specific to your application.
For example:
# /milan/.htaccess
RewriteEngine On
# Forward all requests to the "public" subdirectory
RewriteRule (.*) public/$1 [L]
# /milan/public/.htaccess
RewriteEngine On
# Rewrite "auth/<something>/" to "auth/index.php?page=<something>"
RewriteRule ^auth/([^/]+)/$ auth/index.php?page=$1 [QSA,L]
The .htaccess file at /milan/public/.htaccess also serves to prevent a rewrite loop when requests are rewritten to the public subdirectory by the .htaccess file in the parent directory. This is because mod_rewrite directives are not inherited by default.
The QSA flag is only required if you are expecting query strings on the original request.
The RewriteRule pattern (1st argument) matches the URL-path relative to the directory that contains the .htaccess file.
RewriteRule ^/(.*)$ /public/$1 [R=301,NC,L]
RewriteRule ^public/auth/([a-zA-Z]+) /index.php?page=$1 [QSA,L]
A few notes on your attempt - which is close, but has a few crictical errors:
The URL-path matched by the RewriteRule pattern does not start with a slash (when used in .htaccess), so the regex ^/(.*)$ will never match.
The first rule is also an external redirect (ie. exposes the /public subdirectory) which doesn't seem right. Do you really want /public in the visible URL - if so then you should be linking directly to the /public subdirectory, not relying on a redirect?
The first rule is redirecting to /public in the document root, not /milan/public.
Once corrected, the first rule will also result in a rewrite-loop (500 Internal Server Error) as it will repeatedly rewrite the request... public/public/public/<something> etc.
The second rule is also rewriting to /index.php in the document root, not /milan/public/auth/index.php.

how to rewrite single URL using .htaccess

I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]

Apache rewrite rule works on localhost but not on server

I have a site that I would like to use Apache's RewriteRule to rewrite URLs.
I want:
http://baileyseymour.com/index.php?p=home
to rewrite to
http://baileyseymour.com/p/home
I have AMPPS installed on my Mac and I added the following lines to httpd.conf and they work successfully:
RewriteEngine On
RewriteRule ^/p/(.*) /index.php?p=$1 [PT]
I'm trying to do the same but on my server.
And I have added the same apache code to /public_html/.htaccess but I get the error message below:
Not Found
The requested URL /p/home was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
The exact same code works on my localhost server. Why not on my website?
Can you check your remote server apache supports "AllowOverride All" ?
also try this way maybe it will help.
RewriteEngine On
RewriteRule ^p/(.*) /index.php?p=$1 [PT]
but you may have to modify $_GET['p'] properly. which will be sent only "home" part.
You need to remove the leading slash from the rewrite rule's pattern. URI's have their leading slash removed when the rewrite engine applies rules in an htaccess file.
RewriteEngine On
RewriteRule ^p/(.*) /index.php?p=$1 [PT]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.baileyseymour\.com
RewriteRule ^(.*)$ http://www.baileyseymour.com/$1 [R=301,L]
</IfModule>
.htaccess is the right place to put them and that file should be in the same directory as your default home page.

URL rewrite not working when another htaccess is in a subfolder

I have an .htaccess file in the root of my site. Inside the file, I have a code that redirects all traffic to add "www." to the beginning of the url:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.com [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]
This works great for 95% of the site. However, I have one subdirectory, website.com/playdate, that has another .htaccess file inside the folder. Inside this .htaccess file is the following code, to redirect the URL to something a little cleaner:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^page/([0-9]+)/ index.php?page=$1
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/ view.php?url_slug=$1
It seems like this code is overriding the www redirect. I have tried to add the initial code,
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.com [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]
to the top of the secondary .htaccess, but this does not work.
How do I get the subdirectory to continue to forward to the "www." version of the site, while also keeping the secondary .htaccess rules for this subfolder?
When a request is made, for example /playdate, the first thing that happens is a URL to file mapping is made, then apache checks if that file mapping, which in this case maps to a directory, contains an htaccess file. If it doesn't, then it checks parent directories for htaccess files. If it does, then it simply uses the htaccess file in that directory.
So when a request is made for /playdate, it has an htaccess file in that directory so apache doesn't bother looking for on in any parent directories. Only this one htaccess file is applied, any ones sitting in parent directories are ignored.
If you are using apache 2.2, then you'll just have to copy over the www redirect rules, but you'll need to tweak them because your no longer in the document root. Add these rules to the top of your htaccess file (or at least, above any rules that you already have in there):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.com [NC]
RewriteRule ^(.*)$ http://www.website.com/playdate/$1 [L,R=301]
If you are using apache 2.4, there is a specific option you can apply to the htaccess file in the playdate directory:
RewriteOptions InheritBefore
This makes it so any rules in the parent scope are inherited before any rules in the child scope. This is important because you need to make sure the www redirect rules occur before any of your routing rules are applied, otherwise the URI gets mangled. Apache 2.2 has a similar option, Inherit but those are placed after any rules in the child scope.
I think your problem is the L flag you are using when you rewrite the full domain name.
[L,R=301]
The L flag means "Last", as in, "this is the last rewrite directive I want the server to process for this URI request." If the user agent requests "http://website.com/playdate", then the rewrite condition will trigger the line
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]
The server will rewrite the request to http://www.website.com/playdate and stop processing rewrite rules for that URI request because the server comes to the L flag.
At least that is my guess.

URL rewriting-page not found error

In order to convert my dynamic URL i.e www.3idiots.co.in/index.php to static url i.e www.3idiots.co.in/index.html, I edited my .htccess file and put the following code in it:
RewriteEngine On
RewriteRule ^index.php$ /index.html [R]
when i uploaded this file in the root directory,and try to open the page, I got the error
404 page not found error, www.3idiots.co.in/index.html not found.
You have to actually have a file named index.html. Right now you don't. The rewriting/redirecting is working fine, you're just redirecting to a non-existent page/file.
I'm a little confused as to what you're actually trying to do. If you just want to move index.php to index.html, rename the file. Rewriting makes it so that if someone tries to open index.php they will be redirected to index.html, but you still have to have an index.html file for them to be redirected to.
RewriteEngine On
# Send the user to index.html
RewriteRule ^index.php$ /index.html [R]
# Tell the server that index.html really means index.php
RewriteRule ^index.html$ /index.php
Try these rules:
RewriteCond %{THE_REQUEST} ^GET\ /index\.php
RewriteRule ^index\.php$ /index.html [L,R=301]
RewriteRule ^index\.html$ index.php [L]
The first rule redirects every direct request of /index.php externally to /index.html. And the second rule rewrites requests of /index.html internally to /index.php.
Are you sure mod rewrite is enabled & working?
1) create an html page called found.html with whatever you want in it, but some text to be sure it's loaded (not a blank page basically) and put this in an file called ".htaccess" :
RewriteEngine on
RewriteBase /
RewriteRule ^find.html$ /found.html [L]
2) upload both your .htaccess and the found.html files in your domain's root
3) Just try to load -www.example.com/find.html (with your real domain of course). If mod_rewrite is available, you should see the content of found.html while browsing find.html (which does not physically exist by the way).
If it does not work, try :
RewriteEngine on
RewriteBase /
RewriteRule ^find.html$ found.html [L]
In the Apache Conf file, you also need to make sure that AllowOverride is set to a value that will allow .htaccess to be processed.
Normally it's AllowOverride all
RewriteEngine On
RewriteRule ^index.html$ index.php
RewriteRule ^$index.htm/$ index.php [L]
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index.html$ index.php
Try this code...
It will work for your problem /..
Best Of LUck
If it solve your problem..
Visit my site

Categories