htaccess redirecting to index issue - php

I got a error/issues with my .htaccess codes. The htaccess file is located in a sub-directory "domain.com/subdir/.htaccess". The thing is that when I access "domain.com/subdir/" is ok but when I try "domain.com/subdir/test/run.php" the browser wont proceed to the said url but rather stick to "domain.com/subdir". And if the file/dir does not exists its just go to the "domain.com/subdir" page and the url on the address bar still "doamin.com/subdir". Sorry for the poor english.. My Code below on my htaccess
RewriteCond %{REQUEST_URI} ^(.*)/index.php$
RewriteOptions Inherit
RewriteRule .* index.php [L]
Can anyone show me the right code. Aim is to proceed to any url even is a 404 or not not always sticking to the "domain.com/subdir"

Add this to your .htaccess file
RewriteCond $1 !^(index\.php|test|any_other_directory)

Related

Url rewriting and redirrecting not working (quite)

I have a WordPress installation in my main folder, so my site goes like
www.example.com
Besides that I have a subfolder with a separate static site that you can access with
www.example.com/special
So in the special pages I have a page called
www.example.com/special/my-special-page
It's actually a .php file, but I've removed the extensions in the .htaccess file of that special site with this
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And this works.
But my client said: I want, when I enter
www.example.com/myawardspage
to go to
www.example.com/special/my-special-page
but to have the above URL (without /special/my-special-page) in it. So in the main .htaccess file (the one that controls the WordPress) I've added
RewriteRule ^myawardspage?$ http://www.example.com/special/my-special-page [NC,L]
So now when you go to
www.example.com/myawardspage
I am redirected to
www.example.com/special/my-special-page
which is great, but I need the URL to look like
www.example.com/myawardspage
So in the .htaccess file of the special page I've added
RewriteRule ^myawardspage/?$ /special/my-special-page [NC]
But the URL remains the same (http://www.example.com/special/my-special-page).
What am I doing wrong here?
Just below RewriteEngine on, place the following lines in your /.htaccess (document root) file:
# Check if the file being requested exists
# in the 'special' directory
RewriteCond %{DOCUMENT_ROOT}/special/$1.php -f
# If so, then rewrite accordingly:
RewriteRule ^([^.]+) special/$1.php [L]
Using this method means that you don't need that rule in the special/.htaccess file - you can safely remove it.
Ok, so you mean when ever you want to hit www.example.com/myawardspage then you want to get data from www.example.com/special/my-special-page while in your address bar your address remains www.example.com/myawardspage To achieve this apply this rule in your WordPress installation's .htaccess file.
RewriteRule ^myawardspage/?$ www.example.com/special/my-special-page [NC,L] # Handle requests for "www.example.com/special/my-special-page"
It wil get you desired data and your problem will be solved. Let me know if that works for you.

Rewrite everything after a certain directory to that directories index.php (htaccess)

Hi, so this may be asked elsewhere but I have searched and come up with irrelevant results.
I clearly don't know what to search for exactly.
I'm just trying to rewrite everything after a certain directory to that directories index.php.
Here is an example of the URL a visitor would SEE
website.com/search/location/United%20States
And I would like that URL to be rewritten server-side so that it loads website.com/search/location/index.php
(not a 301 redirect)
I would like the Url to stay the same but load the index.php script (to include United%20States so this can be passed to PHP to determine what the location is and if it is legitimate etc.).
Sorry I know that this will be somewhere already but I just can't find it
I have some code already but it is buggy and seems to choose when it wants to work and also sometimes uses location/index.php/United%20States which is not what I want.
Put this code in your htaccess (which has to be in your root folder)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/location/.+$ /search/location/index.php [L]
If you have Apache web server, make sure you have mod-rewrite enabled and put .htaccess file into your WEBROOT/search/location directory. Put this into .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]
This will internally redirect all requests, where file or directory does not exists, to index.php.
You could also put .htaccess file into your WEBROOT directory and write this into to:
RewriteRule ^search/location/.* /search/location/index.php
Hope this helps.

Replace part of filename in URI with htaccess mod_rewrite x-domain solution required

I have been trawling the tutorials on htaccess but every example seems to have an absolute URL in the second part of the rewrite. My site delivers across several domains so I need a x-domain solution.
Rule should state the following: Everything that did go to the confirm.php file now goes to confirm/ folder. So...
http://example.com/confirm.php?token=32879 should now go to http://example.com/confirm/?token=32879
AND
http://elpmaxe.com/confirm.php?token=32879 should now go to http://elpmaxe.com/confirm/?token=32879
The code I have come up with is:
RewriteCond %{REQUEST_URI} confirm.php$ [NC]
RewriteRule ^(confirm\.php)$ confirm/ [L,R=301]
But this appends the entire document root path to the end of the URL as follows:
http://example.local/var/www/vhosts/example.co.uk/confirm/?t=CB3Qj&e=578
Please help!
This rule should work:
RewriteEngine On
RewriteRule ^(confirm\.php)$ /confirm/ [L,R=301,NC]
Make sure to test in a new browser to avoid 301 caching issues.

Redirect rules for 404 content using .htaccess

It's quite common for us to put in progress sites at www.domain.com/dev/ and then once the client has signed off the site to move it to the top level www.domain.com, what we like is to be able to put a .htaccess file in the top level so that once we've moved the site out of /dev if the client accidently goes to www.domain.com/dev/apage.php that they be redirected to www.domain.com/apage.php, but only if www.domain.com/dev/apage.php doesn't exist.
Sometimes the dev folder will be called various other things, and ideally we don't want to have to edit the .htaccess file to match the folder name.
Thanks for any help.
You could do something like
Edited based on comments:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule dev/(.*)$ $1 [R=301,L]
Which basically means that if the file doesn't exist - RewriteCond %{REQUEST_FILENAME} !-f - then rewrite any request to dev back to the root. You have to specify dev/ in the rewrite rule as otherwise you will get stuck in a redirect loop.
This will only work however if you are using explicit files rather than a framework with everything routed through index.php for example

Two htaccess files

I have a question about using multiple .htaccess files - I couldn't find the answer to this after looking elsewhere on stackoverflow, so I hope you guys can help.
I currently have one .htaccess file in the root of my site, which performs a simple url rewrite:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [L]
ErrorDocument 404 /index.php
I'm currently working on the second phase of development of this site, and I've made a replica in a subfolder (e.g. www.abcdef.com/new/). The trouble is, at the moment if I click a link on this replica site, it redirects me to the root, original page, whereas I want it to go to the equivalent page in the new/ folder. I've put another .htaccess file in this new/ folder, which however doesn't have any noticeable effect:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /new/index.php?url=$1 [L]
ErrorDocument 404 /index.php
So my question is: is it permissible to have another .htaccess file in a subfolder like this? And if so, why aren't the above lines working?
Thanks in advance for any ideas on this!
It's possible to have multiple .htaccess files, and the system is designed to work the way you want it to.
You're setting RewriteBase, which explicitly sets the base URL-path (not filesystem directory path!) for per-directory rewrites.
So it seems like your requests would be rewritten to /new/new/index.php, a path and directory which probably doesn't exist on your filesystem (thus not meeting your RewriteConds) and such is being redirected to your /index.php 404.
As a test, perhaps try changing the ErrorDocument to:
ErrorDocument 404 /new/index.php
If you see rewritten calls go to this then it might indeed be your RewriteBase.
You say
The trouble is, at the moment if I click a link on this replica site,
it redirects me to the root, original page, whereas I want it to go to
the equivalent page in the new/ folder.
Could it be that you are using absolute links in your pages and not relative ones? For instance if a link looks like "/sample", when in your main site it will link to http://.../sample and the same is true if the link is inside a page under "/new/". If you'd use just "sample" then that would resolve as http://..../sample or http://...../new/sample, depending on the URL of the page.
Having a second htaccess file in a subdirectory shouldn't be an issue, and as far as I can tell, your two look okay.
Are you sure the links in the site are correct? (ex, they are /new/foo, not just /foo)?

Categories