I use .htaccess for Friendly URL, but i have a problem. My subdomain store.domain.com redirect to "/" but, default file is "store.php" and domain.com redirect to "/", default file is "index.php"
i need use if, for example
<If host == 'store.domain.com'>
store rewrite rules
<If host == 'domain.com'>
other rewrite rules different of store
</if>
How i do in .htaccess??
My Htaccess:
IndexIgnore *
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ store.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ store.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&action=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?page=$1&action=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ store.php?page=$1&action=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ store.php?page=$1&action=$2
Please try and research your problem a little more before posting a question on Stack Overflow next time. A simple web search of your problem brings up RewriteCond statements. Then you could refer to the Apache mod_rewrite documentation.
This documentation would lead you to results, such as:
RewriteCond %{HTTP_HOST} ^store.example.com$
RewriteRule ^([a-zA-Z0-9_-]+)/?$ store.php?page=$1
Related
One of my website's old URL structures is like this:
https://example.com/login.php
or,
https://example.com/login.php?redirect=https://example.com
or,
https://example.com/register.php
after rebuilding the URLs, the URLs look like this:
https://example.com/login
or,
https://example.com/login?redirect=https://example.com
or,
https://example.com/register
Now when the user/visitor uses the old URL https://example.com/login.php, I want to redirect to the https://example.com/login page. How can I do it?
FYI, currently, my .htaccess file has the following content:
AddDefaultCharset UTF-8
ErrorDocument 404 /error-404/
##
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
## Allow a few SEO Files direct access.
RewriteRule ^robots.txt?$ robots.txt [L]
RewriteRule ^ads.txt?$ ads.txt [L]
RewriteRule ^sellers.json?$ sellers.json [L]
## Avoid rewriting rules for the admin section
RewriteRule ^(admin|resources)($|/) - [L]
## Set Ajax Request File
RewriteRule ^kahuk-ajax.php?$ kahuk-ajax.php? [L,QSA]
## Set controller with id
RewriteRule ^([^/]+)/([0-9]+)/?$ index.php?con=$1&id=$2 [L,QSA]
## Set controller with slug
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?con=$1&slug=$2 [L,QSA]
## For paging
RewriteRule ^([^/]+)/page/([0-9]+)/?$ index.php?con=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/page/([0-9]+)/?$ index.php?con=$1&slug=$2&page=$3 [L,QSA]
## Set controller for only one parameter
RewriteRule ^page/([^/]+)/?$ index.php?con=page&slug=$1 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?con=$1 [L,QSA]
## Set home page
RewriteRule ^/?$ index.php?con=home [L]
You can insert this rule just below RewriteBase / line to remove .php extension:
# To externally redirect /afile.php to /afile
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule !^admin/ /%1 [R=301,NE,L,NC]
I want to create pretty url. But, I got some problem with .htaccess. For example I have url domain/some.php?f=query-string.
I want to change domain/query-string (expected url). Is that possible to change / redirect via .htaccess. Or maybe from php file itsself.
this is a bit of htaccess snippet i made, but i get it blank/error page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
</IfModule>
Thanks for your attention.
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash, so the above will never match and it will do nothing. This should be written like the following instead:
RewriteRule ^([^/.]+)$ some.php?f=$1 [L]
The NC flag is not required here, since the regex is already "case-insensitive".
hi i have use many different htaccess codes, but i cant get rewrite to work.
i want this url
https://domain.com/category.php?cat=firm
to look like this url
https://domain.com/category/firm
this is my latest attempt
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.html$ /category.php?cat=$1 [L]
ErrorDocument 404 https://seoboost.no/404page.php
RewriteCond %{HTTP_HOST} ^www\.seoboost\.no$
RewriteRule ^/?$ "https\:\/\/seoboost\.no\/" [R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F]
RewriteRule ^index\.php$ / [R=301]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301]
i have try to delete all in my htaccess and only have this code
RewriteEngine on
RewriteRule ^([^/]*)\.html$ /category.php?cat=$1 [L]
but it still not working, is my server or what am i doing wrong??
Try replacing your .htaccess with
RewriteEngine On
RewriteRule ^category/(.*)$ /category.php?cat=$1 [L]
which will redirect yoursite.com/category/12 to yoursite.com/category.php?cat=12 internally, and the user will never see the "ugly" url. Then inside your category.php file you can access the cat by $category_id = $_GET['cat']
A simple anchor tag looks like this:
Item 12
Pulling from Justin Lurman's answer here, the following should work:
RewriteEngine On
# redirect "/category.php?cat=..." to "/category/..." to hide a directly accessed "ugly" url
RewriteCond %{THE_REQUEST} \s/category\.php\?cat=(.+)\s [NC]
RewriteRule ^ /category/%1? [R=301,L]
# internally rewrite "/category/..." to "/category.php?cat=..."
RewriteRule ^category/(.+)$ /category.php?id=$1 [L]
As Justin noted in that answer to a similar question, you need to confirm that htaccess files are enabled/allowed in your Apache configuration and that mod_rewrite is enabled.
Additionally, I am sure you are aware, but just in case you are not, after implementing this new htaccess redirect rule, you should change all of the links on your webpages to use clean/friendly URLs. By doing so, neither your users nor a search engine crawler will access the "ugly" URLs unless they access them directly (via a bookmark or a saved link).
It's very strange to me, but:
I have a website with guestbook-add.php. I want to show it to the visitors as gasterbuch.html.
So I have written in .htaccess (my real domain has been changed in that listing to http://mywebsite.com/ of course):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ http://mywebsite.com/index.php [L]
RewriteRule ^gasterbuch.html$ http://mywebsite.com/guestbook-list.php
RewriteRule ^eintrag-hinzufugen.html$ http://mywebsite.com/guestbook-add.php
</IfModule>
Now, when I type in the browser http://mywebsite.com/gasterbuch.html, I see the text generated by guestbook-add.php (which is fine).
But in the browser url bar, I see http://mywebsite.com/guestbook-add.php instead of http://mywebsite.com/gasterbuch.html (like it was the 302 redirect).
What am I doing wrong?
Changing
RewriteRule ^index.html$ http://mywebsite.com/index.php [L]
RewriteRule ^gasterbuch.html$ http://mywebsite.com/guestbook-list.php
RewriteRule ^eintrag-hinzufugen.html$ http://mywebsite.com/guestbook-add.php
to
RewriteRule ^index.html$ /index.php [L]
RewriteRule ^gasterbuch.html$ /guestbook-list.php [L]
RewriteRule ^eintrag-hinzufugen.html$ /guestbook-add.php [L]
should work.
From last 3 hours, I'm trying this 301 url redirect but its not working as expected. Please help me with this. here is the .htaccess file code.
Options +FollowSymLinks +SymLinksIfOwnerMatch -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteBase /
rewriterule ^(.*)/(.*)\.html$ product.php?id=$1&name=$2 [L]
rewriterule ^deals/(.*)$ details.php?id=$1 [L]
rewritecond %{SERVER_PORT} 80
rewritecond %{REQUEST_URI} publisher.php
Redirect 301 /deals/74/product-name.html http://mydomain.com/74/product-name.html
Whenever I enter www.mydomain.com/deals/74/product-name.html, It redirects me to "www.mydomain.com/deals/74/product-name.html?id=74&name=product-name"
I'm not sure why its appending "?id=74&name=product-name" after url? I want to display only "www.mydomain.com/deals/74/product-name.html"
I don't know how to fix this problem. I'll appreciate if you can guide me on this.
I think it's because you're using (.*)/(.*).
This is what I always use:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [QSA,L]
</IfModule>
All URLs start checked and rewrited by PHP (or use nginx if you want speed), but .htaccess must be clean. Simple for read, simple for rewrite (to nginx or another server).
index.php:
if (substr($_SERVER['HTTP_HOST'], 0, 4) != 'www.'){
$protocol = (substr(PHP_SAPI, 0, 3) == 'cgi' ? 'Status:' : $_SERVER['SERVER_PROTOCOL']);
header($protocol.' 301 Moved Permanently');
header('Location: http://www.example.com'.$_SERVER['REQUEST_URI']);
exit;
}
// Simple path checker
$uri = trim($_SERVER['REQUEST_URI'], '/');
$path = pathinfo($uri);
// Check
if ($path['extension'] == '.html'){
$_GET['id'] = $path['dirname'];
$_GET['name'] = $path['filename'];
include 'product.php';
exit;
}
if ($path['dirname'] == 'deals'){
$_GET['id'] = $path['filename'];
include 'details.php';
exit;
}
Look at the Redirect directive docs at: http://httpd.apache.org/docs/current/mod/mod_alias.html#redirect
There you can see that:
If the client requests http://example.com/service/foo.txt, it will be told to access
http://foo2.example.com/service/foo.txt instead. This includes requests with GET parameters,
such as http://example.com/service/foo.pl?q=23&a=42, it will be redirected to
http://foo2.example.com/service/foo.pl?q=23&a=42. Note that POSTs will be discarded.
The last rule in your config is:
Redirect 301 /deals/74/product-name.html http://mydomain.com/74/product-name.html
mod_alias will append all GET parameters
If you don't want the GET parameters to be appended, maybe you might change the Redirect rule for a Rewrite rule
Something like:
Rewrite ^/deals/74/product-name.html http://mydomain.com/74/product-name.html [R=301]
Easy
I'm not sure why its appending "?id=74&name=product-name" after url?
It is doing so because your URI /deals/74/product-name.html is matching both these rules:
RewriteRule ^(.*)/(.*)\.html$ product.php?id=$1&name=$2 [L]
and
Redirect 301 /deals/74/product-name.html http://mydomain.com/74/product-name.html
It is not a good idea to mix mod_alias and mod_rewrite together. Better to use mod_rewrite itself for finer and granular control.
Your modified .htaccess:
Options +FollowSymLinks +SymLinksIfOwnerMatch -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteRule ^deals/(74/product-name\.html)$ /$1 [L,R=301,NC]
RewriteCond %{REQUEST_URI} !/74/ [NC]
RewriteRule ^([^/]+)/([^.]+)\.html$ /product.php?id=$1&name=$2 [L]
RewriteRule ^deals/(.*)$ /details.php?id=$1 [L]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} /publisher.php [L]
First of all thanks to everyone who responded and tried to help me. It took approx. the whole day to figure out myself the solution. I hope my answer will help someone,
Options +FollowSymLinks +SymLinksIfOwnerMatch -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^deals/74/product-name.html$ 74/product-name.html [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteRule ^(.*)/(.*)\.html$ product.php?id=$1&name=$2
rewritecond %{SERVER_PORT} 80
rewritecond %{REQUEST_URI} publisher.php
RewriteRule ^(.*)$ publisher.php [L]
I took the last line "RewriteRule ^deals/74/product-name.html$ 74/product-name.html [R=301,L]" and pasted above all Rewrite Rules and it worked. I guess it was overlapping with some other Rewrite rules. But finally, its working as I expected.
Thanks to Everyone again. :-)