Hey, I am trying to use mod_rewrite to make http://example.com/style/universal.css show the page http://example.com/style.php?n=universal
I am currently using the following RewriteRule:
RewriteRule ^style/([^/\.]+)\.css$ style.php?n=$1
But it doesn't seem to work, for some reason it instead shows a 404 Not Found which is not even the correct 404 page.
I have tried doing it without the extension .css (i.e. http://example.com/style/universal) with the RewriteRule
RewriteRule ^style/([^/\.]+)$ style.php?n=$1
Which works, but I would much prefer it if I could get it working with the .css extension.
It seems to be something with the server ignoring the RewriteRule if a file extension is used. Is there a RewriteCond or something obvious that I am missing?
I should also mention that there is a .htaccess in the parent directory which is set by WordPress, the contents of this are:
RewriteEngine on
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any help would really be appreciated.
Thanks in advance, Brad.
Set up your mod_rewrite to log all the steps it does processing your url
RewriteLog "/tmp/mysite_rewrite.log"
RewriteLogLevel 6
reading the logs you should be able to understand how your url is rewritten and what is the problem.
Related
I am a beginner web dev and I am attempting to convert one of my old wordpress sites into a vanilla JS/HTML and CSS website.
The URLs on my wordpress site began with www and had trailing slashes at the end of every url (other than the home page). I have been attempting different commands via the .htaccess file I created and I managed to successfully remove the .html extensions from my pages and add the www prefix. However, despite numerous tries (I have tried around twelve different snippets that I found on this website and others) I have not been able to add trailing slashes to the URLs.
I would appreciate any help. Thanks in advance.
The following is my current .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
My goal is to force HTTPS on the admin directory of my site. However, using the usual way give an unusual result. Here is the content of .htaccess:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} admin
RewriteRule ^(.*)$ https://www.domain.com/admin/$1 [R,L]
However, when I try to open http://www.domain.com/admin/home.php, instead of redirecting to https://www.domain.com/admin/home.php it sends me to https://www.domain.com/admin/admin/home.php.
So it sort of creates the admin/ part of the URL twice.
I tried changing the last line of .htaccess to RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L] (which doesn't make sense to me, but tried it still) and it redirected to the same wrong URL, which confused me further.
I'm not sure if it should matter, but I've also tried replacing [R,L] with [L,R=301] as suggested elsewhere on SO, with no avail.
Why is this happening and what's the solution?
It is due to /admin/ in your target URL. Change your rule to this:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^admin(/.*)?$ https://www.domain.com%{REQUEST_URI} [R,L,NC]
I have a directory of .png images www.example.com/i/image.png which is located in /var/www/html/example.com/i/image.png
I however, also have some other .png images www.example.com/css/glyph/image.png which is located in /var/www/html/example.com/css/glyph/image.png
I would like to be able to access the images in the /i/ folder from the root directory.
Example www.example.com/image.png will actually take me to www.example.com/i/image.png
Does anyone have any kind of .htaccess solution that would allow me to do this?
Answers to this questions should help - Redirect with htaccess for images onto another server without redirect looping
Specifically:
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^.*\.(gif|jpg|png)$ http://imgserv.example.com/forums/$0 [L,R]
You can use this code in root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+\.png)$ /i/$1 [L,NC]
So, I've this problem:
Base Website located at http://example.com/
Second Website located at http://example.com/web2/
People making various requests to the second website like
http://example.com/myWeb/pg1 and http://example.com/web2/pg2
Recently and due to some other issues I need to have a custom new path for the second website but also keep the first one working.
The ideia is to allow users to access the second website over the two following addresses:
http://example.com/web2/
http://example.com/alternative-url-web2/
The folder /web2/ actually exists on the server, but how can I simulate the folder /alternative-url-web2/ and "redirect" the requests to /web2/?
Please note I don't want the URL on the browser to change, this must be a "silent redirect". And I also make sure that all other requests like http://example.com/other are not redirected by the second website.
Thank you.
Update:
According to #anubhava I could simply solve this issue by adding in my .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
This is probably working fine but I noticed the following:
http://ex.com/alternative-url-web2 is redirected to http://ex.com/web2/ (changing browser URL);
http://ex.com/alternative-url-web2/ is redirected to http://ex.com/(changing browser URL);
http://ex.com/alternative-url-web2/someRequest works fine and does NOT change the browser URL;
http://ex.com/alternative-url-web2/index.php works fine and does NOT change the browser URL;
Site Note:
At /web2/ there's an .htaccess that might be cause the wired redirect behavior above... So here is the file contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
Can the internal RewriteRule to index.php be causing all this? If yes, how can I fix it?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
Alternate code:
RewriteRule ^alternative-url-web2/?$ /web2/ [L,NC]
RewriteRule ^alternative-url-web2/(.+)$ /web2/$1 [L,NC]
This is a pretty simple rewrite. In the htaccess file in your document root, just add the following:
RewriteEngine On
RewriteRule ^alternative-url-web2/?(.*)$ /web2/$1 [L]
Unlike a redirect, which makes the browser/client send a new request for a new URL (thus changing what's in the browser's location bar), a rewrite happens entirely on the server's side.
By the way, in order to follow the trail of htaccess redirects, you could add something like this to each of them:
Header add X-Remark-Rewrite "/path.to/htaccess"
You can inspect these in the response in the developer tools.
I'm helping a client move their blog from a separate Wordpress installation to part of the overall Expression Engine installation for their new (and very large!) website.
The old url structure for the blog was www.site.com/blog/yyyy/mm/foo-bar-title
The new URL structure will be www.site.com/blog/article/foo-bar-title
The .htaccess file isn't that complex so far, essentially it's this:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png|xml)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
And try as I might I can't seem to find a rewrite method that gets the URL rewritten with a 301 redirect without causing an infinite loop.
So far I have RewriteRule ^(.*)\[0-9]+/[0-9]+/?$ /blog/article/ [R=301] but that causes the infinite loop. I've looked at the other questions and answers, but they all seem to deal with just Wordpress (when you search for wordpress), although strictly speaking this won't involve wordpress at all and just Expression Engine.
All help very much appreciated!
You may find it helpful to turn on Apache Server's rewrite log while you tinker with this.
I think you are looking for something like this:
RewriteRule ^index.php/blog/[0-9]+/[0-9]+/(.*)$ /blog/article/$1 [R=301]