Rewrite rules error - .htaccess

I want to rewrite a simple url, but without generating google errors
This code works :
RewriteEngine on
RewriteRule lieu/([0-9]+).* index.php?com=location&lID=$1 [L]
RewriteRule evenement/([0-9]+).* index.php?eID=$1 [L]
but i want to add R=301 flag for SEO
When i add [R=301,L] :
The requested URL /var/www/mysite/index.php was not found on this server.
I know that R=301 flag must be used with http://
but when i try the url is not rewritting

Apache tries to guess whether a path is a URI-path or a file-path, and it's guessing wrong. When you are internally rewriting, a file-path is perfectly fine, because it's all internal to the server. But when you are redirecting, apache incorrectly guesses that your target (the index.php?eID= ) is a file-path and it gets flagged to be handled by mod_alias as a redirect. By the time the redirect happens, it's malformed as a file-path instead of URI path. That's why you're getting the /var/www/mysite/ bit when you redirect.
Either add a RewriteBase to provide a URI base for relative URIs, or make your target an absolute URI:
RewriteEngine On
RewriteBase /
RewriteRule lieu/([0-9]+).* index.php?com=location&lID=$1 [R=301,L]
RewriteRule evenement/([0-9]+).* index.php?eID=$1 [L,R=301]
or
RewriteEngine on
RewriteRule lieu/([0-9]+).* /index.php?com=location&lID=$1 [L,R=301]
RewriteRule evenement/([0-9]+).* /index.php?eID=$1 [L,R=301]

Related

How can I redirect subdomain.domain.com/* to domain.com/folder/*

I have to make a redirection on a .htaccess file.
I want to access the content of a folder from a subdomain url like this:
subdomain.domain.com/* => domain.com/folder/*
The folder contains pdf files and I want to acces it with this url for example:
subdomain.domain.com/file.pdf
I'm new into htaccess redirection rules and I'm a little lost.
I tried something like this and test it into https://htaccess.madewithlove.com/
RewriteCond %{HTTP_HOST} ^subdomain.domain.com/*$
RewriteRule ^(.*) https://domain/folder/ [L,R=301]
This code works on the tester but on my website it throws me the error : "The connection was reset".
Do you have any idea on it?
UPDATE
Following some advices I try but it doesn't work
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com/
RewriteRule (.+\.pdf)$ https://example.com/folder/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^subdomain.domain.com/*$
RewriteRule ^(.*) https://domain/folder/ [L,R=301]
You are not doing anything with the captured URL-path (ie. (.*)) so this will always redirect to https://domain/folder/ (no file).
I would also question whether this should be a 301 (permanent) redirect. Maybe a 302 (temporary) redirect would be preferable here? Note that the 301 is cached, so you will need to clear your browser cache before testing.
It should be like this:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule (.*) https://domain/folder/$1 [R=302,L]
The $1 backreference contains the URL-path captured in the RewriteRule pattern.
The Host header (ie. the value of the HTTP_HOST server variable) contains the hostname only. There is no URL-path component. (Fortunately /* matches the slash 0 or more times, so it still "worked" in your testing.)
However, if the folder only contains .pdf files then you should be more restrictive and redirect only .pdf requests. For example:
:
RewriteRule (.+\.pdf)$ https://domain/folder/$1 [R=302,L]

Problem with using Rewriterule in htaccess

With RewriteRule I've always cleaned my URLs as following:
RewriteRule ^page/(.*)$ page.php?urlkey=$1 [QSA]
My new host doesn't allow me to use Options +FollowSymLinks and therefore I cannot use the / anymore. So I've changed my RewriteRule to:
RewriteRule ^page-(.*)$ page.php?urlkey=$1 [QSA]
However, I need to redirect all my former URLs to the new version. I tried doing this using the following rule:
RewriteRule ^page/(.*)$ page-(.*)$ [R=301,L]
This is however not working. I've also tried to just make a Redirect in my .htaccess:
Redirect 301 https://www.example.com/page/urlkey https://www.example.com/page-urlkey
This is also not working.
EDIT
As requested the actual code below:
RewriteEngine on
RewriteRule ^citywalk-(.*)$ citywalk.php?urlkey=$1 [QSA]
RewriteRule ^citywalk/(.*)$ citywalk-(.*)$ [R=301,L]
For example the citywalk The Historical Centre has a urlkey the-historical-centre. The old url is citywalk/the-historical-centre.
To test this specific case and other technique:
Redirect 301 /citywalk/the-historical-centre https://example.com/citywalk-the-historical-centre
By visiting https://example.com/citywalk/the-historical-centre no redirecting takes place (the url stays the same in the browser) and no urlkey is found.

Unable to get .htaccess RewriteRule to work

I've been pulling my hair out trying to get a URL rewrite rule to work using .htaccess. Mod_rewrite is enabled and I have managed to get a 301 redirect to work (from /beta to /Beta/) so I know the .htaccess is able to work.
Basically I'm trying to get /Beta/Page.php?id=page&tab=services&tabid=tab1 to become /page/services (and ideally leave out the tabid if it's not going to break the site removing it).
The code I'm working with currently is:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/Beta/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /Beta/Page.php?id=$1&tab=$2&tabid=$3
redirect 301 /beta http://www.example.com/Beta/
Any help would be gratefully received.
Remove the leading slash:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(Beta)/[^.]+\.php\?id=([^&]+)&tab=([^\s&]*)&tabid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=302,L]
RewriteRule ^Beta/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /Beta/Page.php?id=$1&tab=$2&tabid=$3 [L,QSA]
This will externally redirect:
/Beta/Page.php?id=page&tab=services&tabid=tab1
to
/Beta/page/services/tab1
and rewrite same URI internally.
Also .htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.

.htaccess | no redirect if there is an specific parameter

im doing a cms at the moment
now im struggeling with the ajax implementation
i have everything running except a mod_rewrite problem..
RewriteEngine On
RewriteRule \.html index.php [L]
this redirects nothing except html files to index.php
i need a second rule witch checks the REQUEST_URI for a parameter to prevent the full site gets loaded by ajax.
i dont think this is understandable so i just post what i want to achieve^^
RewriteEngine On
RewriteCond %{REQUEST_URI} !(?rewrite=no$)
RewriteRule \.html index.php [L]
i want nothing redirected except html files and also no redirect on url's with "(.html)?rewrite=no" at the end
hope someone can help me since rewrites and regexp are not my stongest stuff
thanks in advance
From the Apache docs:
REQUEST_URI
The path component of the requested URI, such as "/index.html". This notably excludes the query string which is available as as its own variable named QUERY_STRING.
So you are actually looking to match on %{QUERY_STRING} rather than %{REQUEST_URI}. Don't include the ? on the query string when matching its condition:
RewriteEngine On
# Match the absence of rewrite=no in the query string
RewriteCond %{QUERY_STRING} !rewrite=no [NC]
# Then rewrite .html into index.php
RewriteRule \.html index.php [L]

How to create permalink in htaccess

I want to redirect a link to another with .htaccess file in Linux host. Can you help me?
from: http://example.com/examp
to: http://example.com/examp.php
And another one for my other site
from: http://example.com/examp
to: http://example.com/user.php?u=examp
You will need mod_rewrite enabled for this. Start with placing these lines into .htaccess:
RewriteEngine On
RewriteBase /
TBH I'm not 100% sure what do you mean exactly by permalink and how do you want to redirect, so I will provide 2 variants for each URL: rewrite (internal redirect) and redirect (301 Permanent Redirect).
1. This will rewrite (internal redirect) request for http://example.com/examp to http://example.com/examp.php while URL will remain unchanged in browser:
RewriteRule ^examp$ examp.php [L]
2. This will do the same as above but with proper redirect (301 Permanent Redirect) when URL will change in browser:
RewriteRule ^examp$ http://example.com/examp.php [R=301,L]
3. This will rewrite (internal redirect) request for http://example.com/examp to http://example.com/user.php?u=examp while URL will remain unchanged in browser:
RewriteRule ^examp$ user.php?u=examp [QSA,L]
4. This will do the same as above but with proper redirect (301 Permanent Redirect) when URL will change in browser:
RewriteRule ^examp$ http://example.com/user.php?u=examp [QSA,R=301,L]
Useful link: http://httpd.apache.org/docs/current/rewrite/
You will need mod_rewrite enabled for this
from: http://example.com/123
to: http://example.com/index.php?q=123
RewriteEngine on
RewriteBase /
RewriteRule ^/?([-A-Za-z0-9]+)/?$ index.php?q=$1 [QSA,L]
You'll want to look at RewriteRules and know/understand regular expressions. It'll be something like this:
RewriteEngine on
RewriteRule ^(.*)\/examp$ /examp.php [R=301,L]
- and -
RewriteRule ^(.*)\/[a-zA-Z0-9\-\_\.]+$ /user.php?u=$1 [R=301,L]
The latter example will take what's in-between the [] and place it in the $1 variable
Here is a good link to get you started:
http://www.webweaver.nu/html-tips/web-redirection.shtml

Resources