I would like to insert a folder-segment into the URL if the site is requested with an other explicit folder-segment.
Example:
http://www.domain.tld/people/xyz to http://www.domain.tld/en/people/xyz
The premiss for this logic is that it should only rewrite this if the segment /people/ in the original request is given. The appended xyz is variable.
Could anyone solve this?
Try the following near the top of your .htaccess file:
RewriteRule ^(people/.*) /en/$1 [R,L]
/people/ must be present at the start of the requested URL for this to redirect.
Change the R to R=301 if this is intended to be a permanent redirect, only after you have confirmed it's working OK.
Related
What I'd like to do is when a URL like
http://localhost/sandbox/jcsearch/country/countryname
is typed by the user I would like to to point to
http://localhost/sandbox/jcsearch/index.php?country=countryname
but still retain the original clean URL in the address bar ie
http://localhost/sandbox/jcsearch/country/countryname
Is this possible? would it create any kind of redirect loop?
The rewrite would happen as follows:
RewriteEngine on
RewriteRule ^sandbox/jcsearch/([^/]+)/([^/]+)$ sandbox/jcsearch/index.php?$1=$2 [L,NC]
Since, the RewriteRule directive does not have the redirection flag (R) set, it will not change the URL in your browser's addressbar. So, by visiting
http://localhost/sandbox/jcsearch/country/countryname
user will get internally redirected to:
http://localhost/sandbox/jcsearch/index.php?country=countryname
Please note: You have to put the rewrite rules in the htaccess file in your server root directory.
I want to make some url rewrite with htaccess,
http://www.example.com/article.php?post_id=123&title=2014-03-02-today-s-trends
=>
http://www.example.com/article/123/2014-03-02-today-s-trends
but some special rule. if some article click from rss or social share. I want to:
http://www.example.com/article.php?post_id=123&title=2014-03-02-today-s-trends&source=rss
=>
http://www.example.com/article/123/2014-03-02-today-s-trends?source=rss
my rewrite code here
RewriteRule ^article/(\d+)/(.*)\?source=(.*)?$ article.php?post_id=$1&title=$2&source=$3
but get 500 error. How to do it in a right way?
You can just use the QSA flag so that it will append the query string with it if source=rss is on the URL.
Not sure how your ogininal rewrite is but you can try this. It should work with or without source=rss
RewriteRule ^article/(.+)/(.+)$ article.php?post_id=$1&title=$2 [QSA,L]
Then in your code you can check for the source key in the $_GET.
I have searched on google and here for a tutorial to help me rewrite my urls.
I would like somebody to explain what I must write and why.
I have this url:
http://iescup.eu/tournaments.php?tourney[id]=1
http://iescup.eu/tournaments.php?tourney[id]=2
http://iescup.eu/tournaments.php?tourney[id]=3
and so on
I would like to have this url:
http://iescup.eu/#!/tourneys/1
http://iescup.eu/#!/tourneys/2
http://iescup.eu/#!/tourneys/3
and so on
Sincerely
Rune Naundrup Dahl
So you type http://iescup.eu/tournaments.php?tourney[id]=1 in your browser's URL address bar. The request /tournaments.php?tourney[id]=1 gets sent to the server iescup.eu. On that server, these rules in the htaccess file in the document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^tourney\[id\]=([0-9]+)$ [NC]
RewriteRule ^/?tournaments.php$ /#!/tourneys/%1? [L,R=301,NE]
The %{QUERY_STRING} variable is matched against and the numeric ID is grouped and backreferenced by %1. The rule's target has a ? at the end to remove the query string and the NE flag is used so the # doesn't get encoded.
The rule redirects the browser to http://iescup.eu/#!/tourneys/1 thus changing the URL address bar. The browser then sends another request to iescup.eu, /. Note that the #!/tourneys/1 fragment is never sent to the server. Fragments are client side only and is used to determine how content should be dealt with (also used by javascript).
This is a strange one...
A while back I managed to write a .htaccess redirect that worked so that the URL was read like: www.website.com/mt?page=index - and what the real URL of this page was www.website.com/PageParser.php?file=index.php
The problem has been that the FTP system of my webhost hides .htaccess files even though they are allowed and do operate - and so I have checked back on local copies I have of my .htaccess files and none of them have the code as to how this works - and I've forgotten how I did it!!
Essentially, I am using wildcards so that anything after mt?page= will actually be showing PageParser.php?file= but without having the PageParser.php showing within the URL (and this is the important bit, because the index.php on my site root is actually sent through PageParser.php first so that anything which shouldn't be there is wiped out before the end user sees it) - so how can .htaccess redirect/rewrite the URL so that any link to /mt?page= show the file located at /PageParser.php?file= without changing the URL the user sees?
RewriteEngine On
RewriteRule ^(.*)mt?page=(.*)$ $1PageParser.php?file=$2
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^page=([^&]+)
RewriteRule ^mt$ /PageParser.php?file=%1.php [NC,L]
This rule will rewrite (internal redirect) request for /mt?page=hello to /PageParser.php?file=hello.php without changing URL in browser.
Your source URL example (www.website.com/mt?page=index) has index while target URL (www.website.com/PageParser.php?file=index.php) has index.php. The above rule will add .php to the page name value, so if you request /mt?page=hello.php it will be rewritten to /PageParser.php?file=hello.php.php.
If there is a typo in your URL example and page value should be passed as is, then remove .php bit from rewrite rule.
The rule will work fine even if some other parameters are present (e.g. /mt?page=hello&name=Pinky) but those extra parameters will not be passed to rewritten URL. If needed -- add QSA flag to rewrite rule.
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
P.S.
Better write no explanation (I knew it/I did it before .. but now I forgot how I did it) than having these "excuses". While it may be 100% true, it just does not sound that great.
Is it possible somehow using mod_rewrite change the url from http://www.mywebsite.com/company/123/reviews to http://www.mywebsite.com/company-123/reivews?
It's not a redirect. The problem is that the real path is the first one and I need my browser to display the second path. So when the user goes to company-123/reviews the content of the page is displayed from company/123/reviews.
Thank you.
RewriteRule ^/([a-z]*?)-([0-9]*?)/([a-z]*?)$ /$1/$2/$3
I think this will work, the regex does it's job atleast.
Use this rule to rewrite the former URL path to the latter one:
RewriteEngine on
RewriteRule ^([^/]+)-([0-9]+)/([^/]+)$ $1/$2/$3 [L]
But you already need the former URLs in your documents to get this rewriting work.