I want to rewrite my urls to more seo urls using wildcard sub-domains and I am unable to write htaccess conditions and hoping to get some help with it.
I have this url:
http://learntipsandtricks.com/blog/c/magento
I want to rewrite as:
http://magento.learntipsandtricks.com/
Change pagination link:
http://learntipsandtricks.com/blog/92/magento/3
to:
http://magento.learntipsandtricks.com/p-92-3
Finally change article url:
http://learntipsandtricks.com/blog/magento/114/magento-index-management-Cannot-initialize-the-indexer-process
to:
http://magento.learntipsandtricks.com/114-magento-index-management-Cannot-initialize-the-indexer-process
I wrote this:
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com\/p\-(\d+)\-(d+) [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%2/%1/%3/$1 [P]
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com\/(\d+)\-(.*) [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%1/%2/%3/$1 [P]
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/c/%1/$1 [P]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L]
The above code doesn't work. Is it possible using if else in htaccess to check all types of urls and rewrite accordingly?
EDIT:
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteCond %{REQUEST_URI} ^/p-(\d+)-(\d+)$ [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%1/[first part of sub-domain]/%2/$1 [P]
How can I get first part of sub-domain here.
Thank you.
HTTP_HOST doesn't include the URL-path. If you want to match against the URL-path, use REQUEST_URI instead
RewriteCond %{REQUEST_URI} ^/p-(\d+)-(\d+)$
for example.
From RewriteCond Directive
RewriteCond backreferences: These are backreferences of the form %N (0 <= N <= 9). %1 to %9 provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched RewriteCond in the current set of conditions. %0 provides access to the whole string matched by that pattern.
Therefore, if you want to capture both the domain and URL-path components, you must combine HTTP_HOST and REQUEST_URI in one RewriteCond
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^(.+)\.learntipsandtricks\.com/p-(\d+)-(\d+)$ [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%2/%1/%3/$1 [P]
Related
currently, I have the following .htaccess-file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
RewriteRule ^ index.php?file=index [NC,L,QSA]
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
RewriteRule ^language/?$ index.php?file=language [NC,L,QSA]
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
RewriteRule ^about-us/?$ index.php?file=about_us [NC,L,QSA]
As you can see, I have to copy the following 2 lines for every RewriteRule:
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
This works, but I want to know if there is a better solution?
What I want?
For example, when a user visits http://{subdomain-here}.example.com/about-us, the page index.php?file=about_us must show up. For every subdomain, except for the subdomains javascript, css and images.
Edit 1: If a visitors goes to http://javascript.example.com/about-us, the browser should give a 404 error.
Edit 2: a "general" regex (e.g. (.*)) will not work because the name of the file in the URL is not always the same as the original filename. http://subdomain.example.com/about-us should point to index.php?file=about_us (see the _) and not to index.php?file=about-us.
You can apply the opposite logic
RewriteEngine on
# Don't touch anything when it comes to those 3 subdomains
RewriteCond %{HTTP_HOST} ^(?:javascript|css|images)\.example\.com$ [NC]
RewriteRule ^ - [L]
# If we reach this part, this means we're not in the 3 subdomains restriction
RewriteRule ^$ /index.php?file=index [NC,L]
RewriteRule ^language$ /index.php?file=language [NC,L]
RewriteRule ^about-us$ /index.php?file=about_us [NC,L]
You can see that as an equivalent to
if subdomain in (javascript, css, images)
return
do something here for other subdomains
You can use this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
RewriteRule ^(.*) index.php?file=$1 [NC,L,QSA]
or the following
:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(javascript|css|images) [NC]
RewriteRule ^(.*) index.php?file=$1 [NC,L,QSA]
I have a set of mod-rewrite rules which look like this:
RewriteCond %{HTTP_HOST} AAAexample.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.AAAexample.com/folder/AAA [R=301,L]
RewriteCond %{HTTP_HOST} BB-BBexample.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.BB-BBexample.com/folder/BB-BB [R=301,L]
RewriteCond %{HTTP_HOST} CCCCCexample.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.CCCCCexample.com/folder/CCCCC [R=301,L]
Essentially I have a set of domains matching (PATTERN)example.com, where (PATTERN) can contain any combination of letters and hyphens.
Is there a way to condense the above rules into a single set such that I Pull PATTERN as a dynamic variable into the final rewrite URL?
Thanks!
You can use:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)(example\.com)$ [NC]
RewriteRule ^/?$ http://www.%1%2/folder/%1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^.+?games/.+?\.jpg$ [NC] #if does not match then proceed with redirect...
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^.+?games/(.+?)\.jpg$ index.php?g=$1 [L] #will only get to this point if not starting with www.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Above code does not work, what am I doing wrong?
Problem is your use of REQUEST_URI variable that changes to /index.php due to your last rule and when mod_rewrite runs again your first rule happily adds www in the URL.
You should use THE_REQUEST variable, THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{THE_REQUEST} !games/.+?\.jpg [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^.+?games/(.+?)\.jpg$ index.php?g=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
I have this code in my .htaccess to handle subdomains (http://foo.mydomain.com)
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteRule ^(.*)$ filter.php?type=country&country=%1 [L]
The problem is what do i have to do if for example someone puts a link like this:
http://foo.mydomain.com/bar
i want to redirect this to another page different from the filter.php (say otherpage.php) from the previous code, i tried the next code but it isn't working
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com/([^/]+) [NC]
RewriteRule ^(.*)$ otherpage.php?type=country&country=%1 [L]
neither the next one:
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)
RewriteRule ^(.*)$ otherpage.php?pname=$1 [L]
The link is always resolved with the first rule i wrote.
Add a condition to your first group that checks for an empty REQUEST_FILENAME. This will prevent it from matching when there's something other than a bare domain name.
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteCond %{REQUEST_FILENAME} ^/?$
RewriteRule ^(.*)$ filter.php?type=country&country=%1 [L]
I'm trying to redirect one domain to the root and another to a directory. The problem I'm having is the second domain is overwriting the firsts redirection.
Here is what I have.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://example.site.net$ [NC]
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://example2.com [NC]
RewriteCond %{HTTP_HOST} !^http://www.example2.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{REQUEST_URI} !^/example2_directory/
RewriteRule ^(.*)$ /example2_directory/$1
That's because 'example2.com' and 'www.example2.com' are not 'example.site.net', like your .htaccess rule states...
If you want those rules to apply to those specific domains, you need to remove the '!' in front of them. Otherwise you need to explain what specific domains need to point to what, not just 'one domain' and 'the other domain'.
Edit: Also, you're not supposed to include the 'http://' for your HTTP_HOST conditions.
I think you might not realize you're negating your comparisons with "!"
Also, you dont' have to turn the engine on twice.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://example.site.net$ [NC]
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [L]
RewriteCond %{HTTP_HOST} ^http://example2.com [NC]
RewriteCond %{HTTP_HOST} ^http://www.example2.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{REQUEST_URI} !^/example2_directory/
RewriteRule ^(.*)$ /example2_directory/$1