I have a very basic two-page website: the home page, and an about page. What I'm trying to do is use htaccess to rewrite the URLs so that the appear as:
domain.com/ (for the home page)
domain.com/about (for the about page)
In the actual folder structure of the site, the homepage is /index.php and the about page is /about.php (both appear in root).
I've been doing research into using alias but unfortunately my hosting (Dreamhost) doesn't allow access to httpd.config so that's out the window and I'm left with using rewrite rules in the htaccess file.
Since the index.php file will appear in the domain root (http://domain.com/) automatically, I've so far managed to make the about page appear correctly at domain.com/about using these lines:
RewriteEngine On
RewriteRule ^about$ /about.php
I'm also using 301 redirects so that for example, domain.com/about/ (with the trailing slash) also directs back to the /about URL like this:
Redirect 301 /about/ http://domain.com/about
This works great.
However the index.php and about.php files still also show if you go to the correct URL within your browser (eg: domain.com/about.php) so as a result the search engines are seeing (and indexing) two versions of each page! I've set up the correct canonical metadata within each page but this doesn't seem to have had any effect (the canonical metadata have been within the page markup ever since the site went live).
So how would I go about firstly doing a 'blind' rewrite (for want of a more technical term) for the two files so that the URLs look correct, but also effectivly 'block' direct access to the files - ensuring that if someone were to attempt to access the php files directly, the URL would still appear in the visitor's browser as the 'pretty' versions rather than the full file name and php extension?
Any advise would be hugely appreciated, I've been researching this for another couple of days now (I don't think there's anything quite the same as this anywhere on here already) and cannot for the life of me work this one out!
Check $_SERVER['REQUEST_URI'] in the PHP files. If that contains the filename (instead of the URI that you want), then do a 301 or 404 or whatever you want. Else serve the page as usual.
You can also do a
RewriteRule ^about.php - [L,gone]
or
RewriteRule ^about.php /about [L,R=301]
but this has to go before your other RewriteRules. It will send a 410 Gone or a 301 Moved Permanently response if the page is accessed via /about.php. See Apache Module mod_rewrite for the complete documentation of mod_rewrite.
Related
I have installed WHMCS in a subdirectory, domain.com/whmcs
Now I am moving it into the main domain and I will be using custom pages. However, i don't want to have .PHP extension on the custom pages that I created. Ex. domain.com/page.php
I will need to remove .PHP extensions from specific pages and redirect these pages to non .PHP versions.
I just want to double check if this is the right 301 redirect for the search engines, before i publish it:
RewriteEngine on
RewriteRule ^mypage$ mypage.php [L]
Redirect 301 /mypage.php https: //domain.com/mypage
It works perfectly when i type domain.com/mypage.php redirects to domain.com/mypage But i am not sure if it's the right way to do it for the search engines. Currently i use wordpress in the root domain, so basically, I want to match the same links.
Thanks!
Just use this to redirect xyz.php to xyz:
Redirect 301 /xyz.php /xyz
As far as I know about search engines, its better to use a 302 redirect to avoid a 'permanent' issue.
Make sure that /xyz actually shows a valid page. So that you do not mess up with the search engines due to a simple mistake like that.
In your example, you use both mod_rewrite and mod_alias. One of them is enough.
RewriteCond, RewriteRule... are from mod_rewrite
Redirect, RedirectMatch... are from mod_alias
Always, test it once before working on a production server.
I'm working on the complete structure of a web page, and I'm using directories to the url of the site the user can understand the site map, with categories and subcategories. for example. My homepage is www.mantarrayamx.com.
The page I am trying to load is www.mantarrayamx.com/services/seo, but for seo I am using the subdomain seo.mantarrayamx.com to access this directory directly.
I'm using third-party code, for example "font awesome". Unfortunately, the web page loading failed because the links are relative. I try entering in the CSS and JS including of third-party code and yet it still loads with errors. You can see the difference between loading by subdomain and loading by sub-directory here:
mantarrayamx.com/servicios/posicionamientoweb/
posicionamientoweb.mantarrayamx.com/
The question is:
What is the best way to use and manage subdomains and links (../img/)?
For example: How do you do google in your applications:
drive.google.com
mail.google.com
If I have to modify the .htaccess file, please give me an example.
As far as I get your question, you are accessing a subdirectory of your server by using a subdomain. On this subdomain, your data is in the root-directory. I guess you are using absolute links in your app, like:
/service/type/(index.php) or
/about/me/(index.php)
First of all: If you just want to have this for seo-friendlieness and beautiful links, you should definitely use mod_rewrite or the appropriate nginx-config. This saves you from having real subdirectories - you just "fake" them. The following code rewrites all requested URLS to index.php?r=theenteredurl. In PHP (or, if you want, any other processing language of your choice) you can sanitize the URL, analyse it and then server the correct content.
mod_rewrite:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /
RewriteRule ^(.*)$ index.php?r=$1 [L]
Nginx:
location / {
try_files $uri $uri/ /index.php?r=$request_uri;
}
The good thing about this solution is, that the only file that really gets processed is your index.php and you therefor have your app/website tidy and on one place. But be aware: HTML, CSS and JS relative links do NOT work as you might expect with this solution, since they do not see what PHP processes, but only what is in the address-bar of your browser. All relative links are relative to the fake subdirectory. To solve this, you can define a base-url in your HTML-file. All other files loaded in this HTML file will be relative to this url.
If I got you wrong and you really want to have real sub-directories on the one domain and no subdirectories on the other, then you could use the HTML base-tag to define a different base-URL depending on whether you are on the main domain or the subdomain. To find out the latter, try the PHP super-global $_SERVER. Please note, that HTML cannot access something that is out of the public scope - if your ressources are in a higher subdirectory that is not publicly accessable on this subdomain, you have no chance of loading it in HTML files.
I am setting up some mod rewrite redirects for particular URLs; we are launching a new site and need to send a few URLs to the old web site until migration is complete. I have copied the format of another site I did this with, with the exception being that the other site was apache 2.4 and this one is 2.2. Here's my (so far) very minimal .htaccess and the new site, domain.org:
RewriteEngine On
RewriteRule ^services/membership/login/login\.html$ http://oldsite.domain.org/services/membership/login/login.html [L]
I can verify that mod_rewrite is enabled because I can get a log to generate, but I can't make sense of the log. The results of browsing to http://domain.org/services/membership/login/login.html, which is a CMS, is that the domain doesn't change and I get the content of the CMS home page but the css and images aren't loaded. This makes no sense to me. If I got a match on the rule, the redirect should happen and I should get the right result. If I don't get a match I should get a 404 because there is no URL for this.
Simply you are able to use the following in your .htaccess:
Redirect http://domain.org/services/membership/login/login.html http://oldsite.domain.org/services/membership/login/login.html
Or
Redirect /services/membership/login/login.html http://oldsite.domain.org/services/membership/login/login.html
For Reference: http://www.htaccessredirect.co.uk
I have recently assisted in moving a website from a pure development domain to a live site where there used to be a site handled by another CMS system than what we are currently using. Current system is Joomla, but I don't think it matters much for my question.
So with the current site the URLs are rewritten from the standard Joomla format to be stripped of index.php and .html suffix is added in the address, meaning that URLs look like this:
http://example.com/folder/page.html
In the old site handled by another CMS systems the URLs had the following structure:
http://example.com/side.php?id=1
We are a social organisation with many sites linking to us - also quite a few that we are not even aware of - so the problem I need to handle is this: I need to redirect all these dead links on other sites so that they simply get pointed to the root of our site.
Can anyone please explain to me how to make .htaccess redirect as follows:
/side.php?id=* to root of example.com
In this case I mean the * to mean any number as there are naturally alot of pages with different IDs.
It is not of any significance to me if they point at a www. prefix or not.
Thanks in advance for your help, I hope I have not asked a question that's been answered before but my experience with .htaccess is very limited and having searched and tried different solutions didn't do it for me.
In .htaccess in the root folder, add the following:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=\d+
RewriteRule ^side\.php$ http://%{SERVER_NAME}/ [R=301,L,QSD] #Remove the ",QSD" for Apache <2.4.0, or to keep the query string.
The R=301 will tell browsers/search engines that the page has permanently been moved.
A rule like this should work:
RewriteCond %{HTTP_HOST} .*
RewriteCond %{QUERY_STRING} id=\d+
RewriteRule ^side.php http://example.com [L,R=301]
This will redirect externally, with a HTTP 301 response (Moved Permanently)
What you need here is some 301 Moved Permanently responses to let browsers and search engines know that you're moving pages to a new location. Instead of using a mod_rewrite to redirect all requests to this side.php page, I would analyze each page of your old website and determine where all the content has been moved. Armed with this, use the htaccess Rewrite directive to inform browsers of individual pages being moved
Redirect 301 /side.php?id=123 /about_us.html
Redirect 301 /side.php?id=456 /contact_us.html
I recommend this method because it redirects those who navigate to the outdated page to the new page that has similar content to the old one they were requesting instead of just redirecting them all to your home page.
I have changed the CMS to my site and it has rewritten the URLs to be more friendly but I have 20 pages which are linked on different sites across the web. I want when people click those old links for them to be redirected to their new respective posts.
I was at first thinking of using a PHP array with a javascript redirect but now I'm thinking .htaccess mod-rewrite would be best.
Whats the best way to have specific URLs redirect to their new URLs?
Use the .htaccess file for redirects. For example:
# This allows you to redirect index.html to a specific url
Redirect /index.html http://example.com/location/
Add some regex to match your rewrites and send to the dynamically created remote url. For example:
# Dynamic redirect
RedirectMatch 301 yourUrl/\(.*)\$ http://external.com/someUrl.html?dynamicVal=$1
That would send ...yourUrl/123 -> http://external.com/someUrl.html?dynamicVal=123
If you are talking about URLs on your pages, then the best way is to edit all such pages with current proper URLs.
If you are talking about "what to do when someone hits my old URL (from their bookmarks or by clicking a link on another site, for example) then the best way is to redirect (with code 301) such requests to a new location using Apache's URL Rewrite module.
Example:
# Activate Rewrite Engine
RewriteEngine On
# redirect to another page on the same site
RewriteRule ^mypage.php /other/mypage.php [R=301,L,QSA]
# redirect to another page on ANOTHER site
RewriteRule ^mypage.php http://www.anothersite.com/mypage.php [R=301,L,QSA]
Here is the link to Apache's manual for mod-rewrite.
Another useful place -- Mod_Rewrite Forums.
Apache supports different modules to rewrite/redirect requested URIs:
mod_alias with Alias and AliasMatch for internal rewriting, as well as Redirect and RedirectMatch for external redirecting
mod_rewrite with RewriteRule for internal rewriting and external redirecting
The main difference between the mod_alias and mod_rewrite directives is that mod_alias’s Alias and Redirect directives work with path prefix patterns while the other directives (i.e. AliasMatch, RedirectMatch, and RewriteRule) work with regular expressions.
A path prefix pattern is always always matched when it’s a qualified prefix of the requested path and the remaining path segments are automatically appended to the target path. Here’s an example:
Redirect /foo/bar http://other.example/quux
If /foo/bar is requested, it’s getting redirected to http://other.example/quux; likewise /foo/bar/baz is getting redirected to http://other.example/quux/baz. Alias works alike but just internally.
In opposite to that, AliasMatch, RedirectMatch, and RewriteRule work with regular expressions and can be used not just to match path prefixes. Here’s an example:
RedirectMatch ^/foo/bar$ http://other.example/quux
If /foo/bar is requested, it’s getting redirected to http://other.example/quux (just like the Redirect example above). But if /foo/bar/quux is requested, it’s not getting redirected as ^/foo/bar$ doesn’t match /foo/bar/quux.
Futher differences between mod_alias and mod_rewrite are that mod_rewrite can also examine other parts of the URI except the path and even HTTP request header fields. It does also allow more complex operations with multiple conditions.