I have a site and i wish to replace a part of URL, https://example.com/[THISPART]/file.ext.
Example URLs that will be requested on site:
https://example.com/example-page/sw.js
https://example.com/example-page/image.jpg
https://example.com/some-other-page/sw.js
https://example.com/some-other-page/file.pdf
https://example.com/page-with-attitude-4/sw.js
https://example.com/page-with-attitude-4/info.txt
How I want to rewrite them:
https://example.com/content/example-page/sw.js
https://example.com/example-page/image.jpg
https://example.com/content/some-other-page/sw.js
https://example.com/some-other-page/file.pdf
https://example.com/content/page-with-attitude-4/sw.js
https://example.com/page-with-attitude-4/info.txt
In other words, if only sw.js is requested, then rewrite to other URL.
What I have used so far in my htaccess is this:
RewriteRule ^(.*)\/sw.js$ content/$1/sw.js [L]
I've been using http://htaccess.mwl.be/ as a tester and test shows alright, but when I use it on site, it doesn't work. Any help?
Put the folowing code at your main directory .htaccess file :
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} sw\.js
#the line above to match sw.js
RewriteCond %{THE_REQUEST} !content
# the line above to exclude any request including content from the following rule
RewriteRule ^(.*)$ content/$1 [R=302,L,NE]
#the line above to apply redirection for requests that passed previous conditions and redirect any request ended with sw.js
RewriteRule ^content/(.*)$ /$1 [L,QSA]
#the last line is to make internal redirection for original path
After testing , if it is Ok , change 302 to 301 if you wanna make permanent redirection
Made it!
RewriteRule ^([A-Za-z-]+)/sw\.js$ /places/$1/sw.js [L,QSA]
Related
With RewriteRule I've always cleaned my URLs as following:
RewriteRule ^page/(.*)$ page.php?urlkey=$1 [QSA]
My new host doesn't allow me to use Options +FollowSymLinks and therefore I cannot use the / anymore. So I've changed my RewriteRule to:
RewriteRule ^page-(.*)$ page.php?urlkey=$1 [QSA]
However, I need to redirect all my former URLs to the new version. I tried doing this using the following rule:
RewriteRule ^page/(.*)$ page-(.*)$ [R=301,L]
This is however not working. I've also tried to just make a Redirect in my .htaccess:
Redirect 301 https://www.example.com/page/urlkey https://www.example.com/page-urlkey
This is also not working.
EDIT
As requested the actual code below:
RewriteEngine on
RewriteRule ^citywalk-(.*)$ citywalk.php?urlkey=$1 [QSA]
RewriteRule ^citywalk/(.*)$ citywalk-(.*)$ [R=301,L]
For example the citywalk The Historical Centre has a urlkey the-historical-centre. The old url is citywalk/the-historical-centre.
To test this specific case and other technique:
Redirect 301 /citywalk/the-historical-centre https://example.com/citywalk-the-historical-centre
By visiting https://example.com/citywalk/the-historical-centre no redirecting takes place (the url stays the same in the browser) and no urlkey is found.
I created a .htaccess file that redirects .png QR code requests to a PHP QR code generator.
RewriteEngine On
RewriteBase /qr/
RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.png
RewriteRule ^(.*)$ access-code.php?id=%1 [QSA,L]
It works well for the original intent, but also sends EVERY .png request to the same script. I need to alter the RewriteCond to only accept .png files if in the /qr/access-code/* directory.
I placed the .htaccess file in the following directory:
/qr/.htaccess
Example request that works well as intended (not an actual link):
http://mywebsite.com/qr/access-code/12345678.png
Example request that "works", but I don't want it to get redirected:
http://mywebsite.com/qr/img/background.png (not an actual link)
How can I adjust my .htaccess condition to make my rule function as intended?
You can use the following :
RewriteEngine On
RewriteBase /qr/
##If /document_root/dir/foo.png is an existent file##
RewriteCond %{DOCUMENT_ROOT}/access-code/$1 -f
##Rewrite /foo.png to access-code.php?id=foo.png
RewriteRule ^(.+\.png)$ access-code.php?id=$1 [QSA,L]
im doing a cms at the moment
now im struggeling with the ajax implementation
i have everything running except a mod_rewrite problem..
RewriteEngine On
RewriteRule \.html index.php [L]
this redirects nothing except html files to index.php
i need a second rule witch checks the REQUEST_URI for a parameter to prevent the full site gets loaded by ajax.
i dont think this is understandable so i just post what i want to achieve^^
RewriteEngine On
RewriteCond %{REQUEST_URI} !(?rewrite=no$)
RewriteRule \.html index.php [L]
i want nothing redirected except html files and also no redirect on url's with "(.html)?rewrite=no" at the end
hope someone can help me since rewrites and regexp are not my stongest stuff
thanks in advance
From the Apache docs:
REQUEST_URI
The path component of the requested URI, such as "/index.html". This notably excludes the query string which is available as as its own variable named QUERY_STRING.
So you are actually looking to match on %{QUERY_STRING} rather than %{REQUEST_URI}. Don't include the ? on the query string when matching its condition:
RewriteEngine On
# Match the absence of rewrite=no in the query string
RewriteCond %{QUERY_STRING} !rewrite=no [NC]
# Then rewrite .html into index.php
RewriteRule \.html index.php [L]
How can I translate an URL like:
http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL
to:
http://localhost/mySite/OFFERS/ARTICLE/DETAIL
if one parameter is empty it should still work like this:
localhost/mySite/?link=OFFERS&sublink=ARTICLE
localhost/mySite/OFFERS/ARTICLE
Extra problem: There is an enter page under index.php and the rewrite should work with index2.php. Best would be if it would work under localhost and on live system without changes.
Currently I'm using: RewriteRule ^([^/.]+)$ index2.php?link=$1 [L]
But that only works for one parameter and I couldn't improve this code for ages ^^
RewriteRule ^([^/.]+)(/([^/.]+))?(/([^/.]+))?$ index2.php?link=$1&sublink=$3&subsublink=$5 [L,QSA]
Note that localhost/mySite/OFFERS/ARTICLE links to localhost/mySite/?link=OFFERS&sublink=ARTICLE&sussublink= and not localhost/mySite/?link=OFFERS&sublink=ARTICLE.
Should not be a big issue, but make sure the PHP code doesn't us isset($_GET['subsublink']).
Try adding the following to your htaccess file in the mysitedirectory of your site.
RewriteEngine on
RewriteBase /mysite/
# rewrite http://localhost/mySite/OFFERS/ARTICLE/DETAIL to
# http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL
#assumes that index2.php is in the root directory of site
RewriteRule ^([-a-zA-Z0-9])+/([-a-zA-Z0-9])+/([-a-zA-Z0-9])+ index2.php?link=$1&sublink=$2&subsublink=$3 [NC,L]
#redirect index to index2
#if you do not want to redirect, just server rewrite, remove the R=301 below
RewriteRule ^index\.php$ index2.php [NC,L,R=301]
My host will not allow me to change the default folder of my primary domain. I have managed to Rewrite http://www.mysite.com to the real folder
public_html/mysite.com/www/
with the following code:
RewriteEngine On
RewriteRule ^$ /mysite.com/www/ [R=301,L]
This does successfully load my domain from the subfolder, but the url becomes:
http://mysite.com/mysite.com/www/
How can I continue loading requests from http://mysite.com/index.html in the correct folder shown above, without showing it in the client-side url?
Try this one:
RewriteEngine On
RewriteRule ^mysite.com/www/(.*) - [L]
RewriteRule ^(.*)$ mysite.com/www/$1 [L]
UPD:
The line with the dash is required because after the redirect at line 3 Apache reads the .htaccess once again to process the redirected URL. The rule prevents infinite loop.
Try removing the R=301.