how can I do this rewrite with .htaccess - .htaccess

I have an url address like: example.com/?page=3
How can I write the .htaccess rewrite rule, to redirect such pages to example.com/pageid/3
Here "3" is just an example. I would like to make this redirect with any given number.

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)page=([0-9]+)
RewriteRule ^$ /pageid/%2 [L]
Replace the [L] with [L,R] if you want to redirect the browser, thus changing what's in the address bar to example.com/pageid/3 instead of redirecting internally.

Related

condition modification in .htaccess

I need to add redirects to .htaccess.
If the URL address does not contain /site/ or /builder/, redirect to /site/
Example:
http://www.mh.cz/test.php -> http://www.mh.cz/site/test.php
http://www.mh.cz/builder/a.php -> http://www.mh.cz/builder/a.php
http://www.mh.cz/site/contact.php -> http://www.mh.cz/site/contact.php
Actualy .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mh.cz [NC]
RewriteRule ^(.*)$ http://mh.cz/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mh\.cz$ [NC]
RewriteCond %{REQUEST_URI} !/site/
RewriteRule ^(.*)$ http://mh.cz/site/$1 [L,R]
How to modify the condition to redirect be ignored if the URL already exists /builder/ ?
Thanks
Using .htaccess redirect all pages to index.php or whatever you want.
In index.php, grab the requested URL and make conditions to determine which page to include depending on the URL.
For example, http://www.example.com/test.php will display content of http://www.example.com/site/test.php but in the address bar http://www.example.com/test.php will remain, the user will not get Error 404.
I'm not sure if this will fulfill your actual need but it will work.

htaccess rewrite RegEx add static value between parameters

I'm trying to rewrite url's with htaccess. The request url length is variable.
It consists of the following:
http://[domain.com]/[countrycode]/[main-category]/[sub-category]/[sub-sub-category]/[product-name].html
Of course it could also have less categories.
Another option is only a product page, like:
http://[domain.com]/[countrycode]/[product-name].html
I want to rewrite these url's and add a static value between the [countrycode] and the rest of the url path. Also, I want to change .html to .aspx at the end of every rewritten URL.
I came as far as being able to rewrite a url with only the main category like:
http://example.com/en/main-category with this htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^(.*)example\.com [NC]
RewriteRule ^(.*)/(.*)$ https://example.com/$1/3/$2.aspx [R=301]
But if the url contains a trailing slash or more categories, the static value ("3") gets added in the wrong place.
to be clear, I want this structure:
request: http://example.com/en/main-category/sub-category/product-name.html
rewritten: https://example.com/en/3/main-category/sub-category/product-name.aspx
Thanks for your help!
You may use this rule in site root .htaccess:
RewriteEngine On
RewriteRule ^([a-z]{2})/(?!3/)(.+)\.html$ https://%{HTTP_HOST}/$1/3/$2.aspx [L,NC,NE,R=301]

How to .htaccess redirect domain to subdomain while keeping the main domain intact

I want to redirect all urls under my domain
domain.com/urls, www.domain.com/profile/user1.html etc. to subdomain.domain.com/urls and subdomain.domain.com/profile/user1.html etc.
but I dont want to redirect domain.com and www.domain.com to subdomain.domain.com
Is it possible via htaccess?
Edit: I want to redirect just the internal pages and files only. But leaving the main domain.com intact.
More Examples
domain.com/page1.html to subdomain.domain.com/page1.html
www.domain.com/members/admin.html to subdomain.domain.com/members/admin.html
www.domain.com to www.domain.com (no redirection here)
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/.+
RewriteCond %{REQUEST_URI} !^/index\.(php|htm)
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$ [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [L,R]
If you want to permanently redirect, change the square brackets to: [L,R=301].
You can first handle the main page by not rewriting and redirect everything else with a second rule
RewriteRule ^$ - [L]
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$
RewriteRule ^.+$ http://subdomain.domain.com/$0 [R,L]
When everything works as you expect, you can change R to R=301.

.htaccess rewrite url to main domain from subdomain

I have a domain like:
this.that.com
(it can also be accessed from:)
that.com/this
What I would like to do is make it so that the server transfers all requests from the two above url's to:
this.com
I need it so if I type in this.that.com
it will auto transfer to this.com
thanks in advance.
To redirect all requests to this.com, match everything, which is not already this.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^this\.com$
RewriteRule .* http://this.com/$0 [R,L]
If you want to redirect to the main page instead, leave out the URL path
RewriteEngine On
RewriteCond %{HTTP_HOST} !^this\.com$
RewriteRule .* http://this.com/ [R,L]
If everything works as expected, you can switch to R=301 redirection.

Taking a site down temporarily with Codeigniter (301 possible with routes.php?)

I'm using Codeigniter with the standard .htaccess rewrite rules so that no /index.php/ is visible within the urls.
Now I recently needed to take the site down temporarily and therefore wanted to redirect everyone to a 'down' page. The following worked:
$route['default_controller'] = "down";
$route['(:any)'] = "down";
But I'm aware that in this case, a 301 is really appropriate.
How and where should I set that? I don't see a way to specify it in the routes.php and was confused about how to do it within the .htaccess because of the existing rules....
RewriteBase /
RewriteCond %{REQUEST_URI} ^_system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^myapp.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond $1 !^(index\.php|resources|files|robots\.txt)
RewriteRule ^(.*)$ index.php/$1
You don't want a 301 redirect. 301 means "Permanent", and this should only be a temporary redirect. And you can do this with htaccess if you add this above all your other rules:
RewriteRule ^ /down.html [L,R=302]
As long as it's above any of the other rules, then any request will get redirected to /down.html. If you don't want to externally redirect the browser (e.g. have the /down.html URL show up in the URL address bar), then remove the ,R=302 bit from the square brackets and the URL address bar would stay unchanged while the content served is from down.html.

Resources