I am trying to change the url that is displayed in the address bar from mysite.com/blog/wedding-hair/ to mysite.com/services/wedding-hair/ using .htaccess.
Using answers from:
https://stackoverflow.com/questions/8713319/assigning-different-name-to-existing-folder-in-url-in-htaccess
rewrite a folder name using .htaccess
Replace directory name in url with another name
I added to the .htaccess file. Here is the .htaccess file, I added the last rewrite rule:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?$ "http\:\/\/www\.mysite\.com" [R=301]
RewriteRule ^blog/(.*)$ /services/$1 [L]
the non-www redirect works but not the blog-services rewrite. I thought maybe I had the directory names reversed but changing them around doesn't work either. I have tried adding and removing /'s around the directory names in all of the different combinations. I tried adding
RewriteCond %{THE_REQUEST} ^GET\ /blog/
before my RewriteRule. Nothing I Have tried has worked, the displayed url remains mysite.com/blog/wedding-hair/
I am sure this is pretty straight forward for someone but I am unable to get this correct. Any help would be appreciated.
When I was working on this yesterday I didn't think about the fact that the blog directory is a WordPress install. Here is the .htaccess file that is in the blog directory:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
I have tried adding my RewriteRule in this file but still no joy.
The problem here is that RewriteRule ^blog/(.*)$ /services/$1 [L] internally rewrites the URI, so that the browser doesn't know it's happening, this happens entirely on the server's end. If you want the browser to actually load a different URL, you need to use the R flag like you are in your www redirect, though it's only redirecting requests to root. If you want it to redirect everything to include the "www", you want something like this:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Then to redirect "blog" to "services", just add the R flag (or R=301 if you want the redirect to be permanent).
RewriteRule ^blog/(.*)$ /services/$1 [L,R]
And, if for whatever reason your content isn't actually at /blog/, you need to internally rewrite it back
RewriteCond %{THE_REQUEST} ^GET\ /services/
RewriteRule ^services/(.*)$ /blog/$1 [L]
But this is only if your content is really at /blog/ but you only want to make it appear that it's at /services/.
Actually, in such case, as you have a specific field in Wordpress options to handle the display of a different url, it CAN'T work with .htaccess is the WordPress rules are executed at the end.
And it would be much simpler to use the field "Site Address (URL)" in the General Settings, and enter "mysite.com/services/"
If you don't do that, in spite of your .htaccess, the WP internal rewriting will use you installation repertory
Related
The website I'm working on is using some cms. I need to add a static website to this. When I put mypage.html in the main directory and go to www.website.com/mypage.html it works. I would like the page to be accessible without '.html' ending. I experimented with editing htaccess files but always end up with error of too many redirections.
What I entered were various combinations, for example
Redirect 301 http://website.com/mypage http://website.com/mypage.html
The htaccess file I'm using looks like this:
:Location /*.php
Use php54
:Location
RewriteEngine On
DirectoryIndex index_prod.php
Options -Indexes
RewriteRule ^.*\.(css|png|swf|js|gif|jpeg|jpg|flv|pdf|doc)$ - [L]
RewriteRule ^net2ftp - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^/?$ plug.html [L]
#RewriteCond %{REQUEST_URI} !=/
RewriteRule ^/?.* index_prod.php
I'm looking for tips or to be explicitly told what and where to put in htaccess file to make it work (if it's possible)
Could you please try following, considering that you want without extension file URLs to be served by html extension files. Also since you didn't mention any specific condition before RewriteRule hence that redirection errors are coming to it, because its keep on redirecting in lack of any condition/check's presence(till its maximum redirection limit is crossed).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [NC,L]
help me please.
I have several php files in root
domain.com/dog.php
domain.com/cat.php
...
When user go to dog.domain.com i need to show domain.com/dog.php but keep dog.domain.com in browser URL
I already have this .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*).domain.com$
RewriteRule .* http://domain.com/%1.php [L]
It works fine and show domain.com/dog.php content when I type dog.domain.com, but now it changes URL in browser to domain.com/dog.php
How to solve it?
P.S. Wildcard DNS record turned ON (already catch all subdomains *.domain.com)
If *.domain.com is defined within the same vhost you have to use a relative RewriteRule there: RewriteRule ^.*$ /%1.php [L]). If not, you have to replace [L] by [P] and make sure that you have mod_proxy loaded. See https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
Last but not least, you might want to update your RewriteCond to be case insensitive: RewriteCond %{HTTP_HOST} ^(.*).domain.com$ [NC]
I'm trying to rewrite some parameters to beautiful links, but for a subdomain / a folder only. Unfortunately I can't get it to work, maybe also because there are some other rewrites in line before...
Heres my code:
<IfModule mod_rewrite.c>
# NON-WWW TO WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# WORDPRESS-BLOG
Options +FollowSymlinks
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# REDIRECT FOR SUBDOMAIN
RewriteCond %{HTTP_HOST} ^subdomain.example.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)(?:/)?$ index.php?cshort=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /index.php?cshort=$1&cid=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)(?:/.*)?$ /index.php?cshort=$1&cid=$2&step=$3 [L]
</IfModule>
Basically only the last part is the one I want to rewrite to change URLs from something like
http://subdomain.example.com/index.php?cshort=abc&cid=123&step=1 to http://subdomain.example.com/abc/123/1
The other rewriting rules for www.example.com shouldn't get affected. Unfortunately my current codes only does the first two rules for the blog and the www, but nothing happens on the subdomain. What's wrong in my code?
When you say that you want to rewrite from http://subdomain.example.com/index.php?cshort=abc&cid=123&step=1 to http://subdomain.example.com/abc/123/1 you mean that you want the user to enter the pretty URL and to have it serve the full URL in the background, not that you want to redirect from the ugly to the pretty URL, right?
In your RewriteRules, what are you trying to accomplish with "(?:/)?"? As written, that doesn't make any sense to me. If you're just trying to match whether or not the directory path ends with a slash, you can do that as follows:
RewriteRule ^([^/.]+)/?$ index.php?cshort=$1 [L]
EDIT: Additional suggestions:
Move the "Redirect for subdomain" section above the "Wordpress Blog" section. Since the Wordpress rule applies to "everything that's not a real file or directory, regardless of domain" that should go last.
RewriteConds only apply to a single RewriteRule that follows them. For each of the three rules you have listed under "Redirect for subdomain", after updating them per the above suggestion, you need to repeat the two RewriteCond lines in front of the RewriteRule.
Ok, so I have to do some work on a client site - and for some reason, they have both the www. subdomain and the empty subdomain in use, with different websites. They do NOT want this.
They want the www. subdomain ONLY (that is, they want the empty subdomain to redirect to www.)
That's how it is set up in Wordpress, supposedly.
Here's my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)(\.htm|\.html)$ /contact [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
#php_flag display_errors on
#php_value error_reporting 8191
Can anyone interpret this? (I am not very familiar with .htaccess files - anyone who has any resources on them would be helpful). How do I make it so that anyone going to http://examplesite.com goes to http://www.examplesite.com, for example.
How are there two different websites on the same domain? I'm looking in the public_html folder and there appears to be NO reference to the other site at all - nothing, no files, nada.
To force using www subdomain, you can add this code :
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^(.*) http://www.domain.com/$1 [QSA,L,R=301]
Here is what those line does :
Check the HTTP_HOST, which must be domain.com
Take the current request and add it after www.domain.com/ ($1 represents the request caught)
Use the generated url as a 301 redirection (permanent redirection) with [R=301] tag
Doesn't read next rules if this one is caught, with [L] tag
Forward GET datas, with [QSA] tag.
You must put this code after those two lines :
RewriteEngine On
RewriteBase /
Which set the Rewrite engine on, and define the base url.
Be careful : redirection doesn't forward POST datas. Be sure alwayse use www subdomain in your code (this can easily be done using a centralized base_url).
Have a look here to understand better htaccess files.
I'm using this rewriterule:
RewriteRule ^page/([^/]*)$ http://example.com/page?q=$1 [QSA,L]
If I go to example.com/page/somePage I get redirected to example.com/page?q=somePage
But I don't want a redirection, what I want is the URL to always be example.com/page/somePage
How to do this?
Thank you
I removed http://example.com but it doesn't work, I get Page not Found.
I am using Wordpress for my site, this is my complete .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^page/([^/]+)/?$ page?q=$1 [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Whenever you specify the http:// at the beginning of the path to be rewritten to, Apache will always force a 301 redirect to the new URL, whether the URL is on the same website or not. Simply removing the http://example.com part should fix your problem.
As for the page not found, is there another RewriteRule somewhere that tells just 'page' to be processed as 'page.php' or something of the sort? Do you have your PHP files saved without extensions?
Well then your problem is you definitely need to remove the [L] flag because you're telling Apache not to process any more RewriteRules for that request, so it never looks at the WordPress rewrites because that rule was already executed and Apache was told that should be the final rule. I would recommend leaving the [QSA] in the line though, that would not affect the overall outcome of your script.