PHP redirect & url rewrite - .htaccess

I have tried already a couple of ways using URL redirect, but I am no good in php.
RewriteEngine on
RewriteRule ^/Folder/([0-9]+)$ example.com/
I have a problem, the sub folders & files of my server are case-sensitive, for example:
www.example.com/Folder/index.html
How can I make them case insensitive, or redirect alternative spellings to:
www.example.com/Folder/
I also want to hide the actual URL of all sub folders and files, e.g. only the below should be shown, not any sub-items.:
www.example.com/Folder
I have no CMS, only static HTML pages.

You can use mod_speling to allow case-insensitive URLs. It is bundled with the default installation of Apache; you just have to enable it in .htaccess:
CheckSpelling On
To hide files, turn directory listings off:
Options -Indexes

Related

Htaccess and redirect all content of folder, but exlude the main folder

I got the following urls:
domain.com/categoryA/articleA
domain.com/categoryA/articleB
I want to redirect:
domain.com/categoryA/articleA -> domain.com/categoryB/articleA
domain.com/categoryA/articleB -> domain.com/categoryB/articleB
but leave it as it is and do not redirect the main folder: domain.com/categoryA/
I tried to use the rule:
RewriteRule ^categoryA/(.*)$ /categoryB/$1 [R=301,NC,L]
but it also redirect domain.com/categoryA/ to domain.com/categoryB/
How to exclude from the above rewrite rule the redirection of the main folder (categoryA), but still redirect all that is in the folder (and then change also the root folder)?
I am looking for a solution that is SEO friendly (I got the same articles in two categories, but want still to have indexed domain.com/categoryA, but the rest only as domain.com/categoryB/xxx.
Best Greetings,
Mat
With your shown samples/attempts, please try following Rules in your .htaccess file. Please make sure to place this rule under your domain redirect rule(if its there), also make sure to clear your browser cache before testing your URLs.
RewriteRule ^categoryA/([\w-]+)/?$ /categoryB/$1 [R=301,NC,L]

Why my rewrite rule in htaccess is working in some case only?

I try to rewrite some of my URLs with a .htaccess file but it didn't work as expected.
This is the rewrite rule in my .htaccess file :
RewriteRule ^(index|administration)/([A-Za-z0-9-]+)(\.php)?$ index.php?c=$1&t=$2 [QSA]
When I go on www.example.com/index/main, I get a 404 error code.
So I try to change my rewrite rule to
RewriteRule ^index.php$ index.php?c=index&t=main [QSA]
Then I go to www.example.com/index.php and the webpage displays perfectly with all the datas in $_GET (c = index and t = main).
So I don't know why my first rule is not working. Let me see if you have any idea.
Is it possible that my server wants to enter the index folder, then the main folder for my first rule without taking care of my .htaccess (www.example.com/index/main) ?
You need to ensure that MultiViews (part of mod_negotiation) is disabled for this to work correctly. So, add the following at top of your .htaccess file:
Options -MultiViews
If MultiViews is enabled (it's disabled by default, but some hosts do sometimes enable this in the server config) then when you request /index/main where /index.php already exists as a physical file then mod_negotiation will make an internal request for index.php before mod_rewrite is able to process the request. (If index.html also exists, then this might be found first.)
(MultiViews essentially enables extensionless URLs by mocking up type maps and searching for files in the directory - with the same basename - that would return a response with an appropriate mime-type.)
If this happens then your mod-rewrite directive is essentially ignored (the pattern does not match, since it would need to check for index.php) and index.php is called without the URL parameters that your mod_rewrite directive would otherwise append.
it perfectly works by disabling the MultiViews Option in my .htaccess
This would ordinarily imply its your script (ie. index.php) that is triggering the 404 (perhaps due to missing URL parameters?), rather than Apache itself?
However, if you were seeing an Apache generated 404 then it would suggest either:
You also have an index.html file, which is found before index.php. .html files do not ordinarily accept path-info (ie. /main) so would trigger a 404.
OR, AcceptPathInfo Off is explicitly set elsewhere in the config, which would trigger a 404 when the request is internally rewritten to /index.php/main (by mod_negotiation).

htaccess to replace url within source code

is there a way to replace a url being called on all pages in the source code via htaccess? I'm unable to change the code on the pages but have access to htaccess.
Example: each page calls a stylesheet form an external url. I want to change this via htacess to point to my own url elsewhere.
I tried this without success:
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule
^http://3rdpartyurl.com/style.css$ http://differenturl.com/style.css [L]
</IfModule>
This worked for a different project successfully when the url being replaced both resided on my own domain but the urls called on all pages are for an external url.
Thank you
If it is a third party URL that isn't on your server, you won't be able to redirect it with any configuration on your server.
You could edit your code to change the URLs.
You could also use an Apache filter to modify the output to substitute the URLs before they are sent to the client.
xlate is the name of the filter, it will be applied to all HTML files and the command that will be executed is sed s#oldURL#newURL#g
ExtFilterDefine xlate mode=output cmd="/bin/sed s#oldURL#newURL#g"
<Directory /var/www/html/stuff>
AddOutputFilter xlate html
</Directory>

Redirecting /folder/ to /file.html in magento

We published an article in the magazine with following url:
http://magnetic-sleep-machine.com/moves
Now we need to make sure when people put that URL they land to
https://magnetic-sleep-machine.com/moves.html
Please help me figure this out! .htaccess or use a magento (1.7) option?
There's a way to redirect using magento, but not sure if that's exactly what you want.
You could also try turning on multiviews, and let mod_negotiation take cure of fuzzy URL-file mapping, in your htaccess file:
Options +Multiviews
Or using mod_rewrite:
RewriteEngine On
RewriteRule ^moves/?$ /moves.html [L,R=301]
Or using mod_alias:
RedirectMatch 301 ^/moves/?$ /moves.html
Okay I found the way to do it using magento itself.
created a new Page
page link named as "moves"
added javascript for redirect to body of the page
next went to system>config>web>
set Auto-redirect to Base URL to NO
Voila, it works now.

Why this RewriteRule doesn't work?

I'm developing a web site containing 3 pages (Home, page2, page3) ... in the second page there is a navigation bar, with 4 items (subpage1, subpage2, ...), that I use to replace the content of the page 2 with url variables! In other words, the second item of the navigation bar in page2 points to:
http://localhost/uk/page2/index.php?pg=subpage2
the item 3 point to:
http://localhost/uk/page2/index.php?pg=subpage3
Now I would like to use more friendly urls via .htaccess!
I've written this:
RewriteEngine On
RewriteRule /uk/page2/(.*)/$ /uk/page2/index.php?pg=$1
in the .htaccess placed in the root!
But doesn't work!
Please help!!!
When you're using .htaccess you don't have the leading slash:
RewriteEngine On
RewriteRule ^uk/page2/(.*)/$ /uk/page2/index.php?pg=$1
G'day,
I'd suggest enabling the RewriteLog config option at a high level to check what's actually happening under the covers.
Has AllowOverides been enabled?</obvious> (-:
Seems like you're out of luck using .htaccess
Unbelievably mod_rewrite provides URL manipulations in per-directory context, i.e., within .htaccess files, although these are reached a very long time after the URLs have been translated to filenames. It has to be this way because .htaccess files live in the filesystem, so processing has already reached this stage. In other words: According to the API phases at this time it is too late for any URL manipulations. - Apache mod_rewrite doc.s (emphasis mine)
It may be the trailing slash at the end, so change this:
RewriteEngine On
RewriteRule /uk/page2/(.*)/$ /uk/page2/index.php?pg=$1
to this:
RewriteEngine On
RewriteRule ^(.*)uk/page2(/?)(.*)$ /uk/page2/index.php?pg=$3
Another thing you should check is that you have AllowOverride set to All in your httpd.conf file, instead of None. If it is set to None, you won't be allowed to do anything with .htaccess.

Resources