Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Just a quick question,
I am making user profiles for my website and would like to include vanity URLS, but the page has a lot of separate information which gets loaded, e.g Followers tab, uploads tab, and each of these have some kind of setting that can be applied like ASC DESC, New/old results etc. What would be the best way to have vanity urls but keep the functionality i require?
http://www.site.com/user.php?id=1&content=Followers&Order=(Different settings for each result)
But i am wondering if it would be better if i should just name the content differently if someone wanted to search for followers + a limitation, for example
http://www.site.com/user.php?id=1&content=NewestFollowers,
rather than
http://www.site.com/user.php?id=1&content=Followers&Order=Newest
You typically want to construct the vanity URL so that it directly maps to query string parameters to the ugly URL. So if you have:
http://www.site.com/user.php?id=1&content=Followers&Order=Newest
You can make your vanity URL look like:
http://www.site.com/u/1/Followers/Newest/
But the /1/ bit is a little ugly, and not exactly vanity, so if you can pass a username through that query string parameter, it would look better:
http://www.site.com/u/jonlin/Followers/Newest/
Then you'd just put some mod_rewrite rules in an htaccess file in your document root that internally rewrites the vanity URL back to the ugly one:
RewriteEngine On
RewriteRule ^u/([^/]+)/([^/]+)/([^/]+)/?$ /user.php?id=$1&content=$2&Order=$3 [L,QSA]
RewriteRule ^u/([^/]+)/([^/]+)/?$ /user.php?id=$1&content=$2 [L,QSA]
RewriteRule ^u/([^/]+)/?$ /user.php?id=$1 [L,QSA]
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a URL that with some parameters. One of the parameters called 'lang' and it looks like this:
https://example.com/example1/?id=1&lang=en
I want to redirect all the current URL with lang=en to lang=gb
https://example.com/example1/?id=1&lang=gb
Note: the example1 folder is an example and it doesn't a constant variable.
every URL contains a different folder name.
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(?:(.*&)?)lang=en(?:(&.*)?)$
RewriteRule ^ %{REQUEST_URI}?%1lang=gb%2 [R=301]
It is a good idea to start with a R=302 temporary redirection and only to change that to a R=301 permanent redirection once everything works as expected. That prevents nasty caching issues.
In general you should prefer to implement such rules in the actual http server's host configuration. Distributed configuration files (".htaccess") should only be used if no other alternative exist. They come with a number of disadvantages.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I have mediawiki set up like this:
mysite.com/w/
I am trying to add the Short URL method like it suggested in the manual here:
https://www.mediawiki.org/wiki/Manual:Short_URL/Apache
I used the redwerks tool to generate the .htaccess rewriting rules:
http://shorturls.redwerks.org/
Although I am on VPS with root access, I find changing the Apache config and Nginx config to be too complicated and way over my head. So I decided to go with the htaccess method and I couldnt get it to work completely. This is what I added the .htaccess file in the root path:
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]
RewriteRule ^/?$ %{DOCUMENT_ROOT}/w/index.php [L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^/?w/images/thumb/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ %{DOCUMENT_ROOT}/w/thumb.php?f=$1&width=$2 [L,QSA,B]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^/?w/images/thumb/archive/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ %{DOCUMENT_ROOT}/w/thumb.php?f=$1&width=$2&archived=1 [L,QSA,B]
And I Added these to the LocalSettings.php
## The URL base path to the directory containing the wiki;
## defaults for all runtime URL paths are based off of this.
## For more information on customizing the URLs please see:
## http://www.mediawiki.org/wiki/Manual:Short_URL
$wgScriptPath = "/w";
$wgScriptExtension = ".php";
$wgArticlePath = "/wiki/$1";
AND
## To enable image uploads, make sure the 'images' directory
## is writable, then set this to true:
$wgEnableUploads = true;
After the above, the redirection sort of works. Meaning, if I go to mysite.com/w/index.php/Page_Title, it redirects to mysite.com/wiki/Page_Title. But the following doesnt work:
1) When I type in mysite.com/wiki, it doesnt find the page and doesnt automatically goes to Main Page like it used to do when I typed mysite.com/w/. So do I need to add a separate Rewrite rule for this?
2) I am also receiving a page not found and its creating a new article page when the url looks like: mysite.com/wiki/index.php. In this case, it is trying to create a index.php article instead of directing me to Main Page.
3) Even when the above rule is on, when I got to pages other than the main article page, such as edit a page or view history, etc.. it still shows the url as mysite.com/w/index.php?title=XYZ&action=edit. Why doesnt it convert all urls to wiki?
4) If a url has a ?title added to it, it doesnt get redirected to wiki. For example, this page mysite.com/w/index.php?title=Main Page, does not become mysite.com/wiki/Main_Page
After seeing the above, now I am thinking if its better to have my wiki installation to mysite.com/wiki/ folder instead of 'w' folder. Infact thats how I had it first but later changed it when I trying to implement the Short URL method since the MW Manual suggests against it. So I need some guidance on how to resolve this please...
These are just some of the issues I've noticed so far. So basically rewrite rule doesnt completely rewrite all the pages. It only rewriting the main article page url by removing the index.php/ from the url and changing w to wiki. But other urls types are still the same and are not rewritten. When I checked the mediawiki website, all of their urls are rewritten to /wiki/ without the index.php even when editing page, etc.. How can I get the same results?
Okay after further searching on the net, this is what I found.
Even when the above rule is on, when I got to pages other than the
main article page, such as edit a page or view history, etc.. it still
shows the url as mysite.com/w/index.php?title=XYZ&action=edit. Why
doesnt it convert all urls to wiki?
This is how its supposed to work. So this means the Main pages, Namespace pages and Special Pages will have this short url while the other pages like edit page, history, etc.. will still show w/index.php/......
If a url has a ?title added to it, it doesnt get redirected to wiki.
For example, this page mysite.com/w/index.php?title=Main Page, does
not become mysite.com/wiki/Main_Page
It only rewrites the article urls and I should never include ?title=$1 or something like it in the rewrite. Including a query will cause MediaWiki's built in handling to be overridden and will create bugs on your wiki due to the fact that Apache's query rewrites are broken. The goal here is to alias paths to /index.php and then let MediaWiki itself take care of parsing and routing the url, based on the configuration in LocalSettings.php.
So basically rewrite rule doesnt completely rewrite all the pages. It
only rewriting the main article page url by removing the index.php/
from the url and changing w to wiki. But other urls types are still
the same and are not rewritten.
I later found out that thats how it is supposed to work.
I am yet to find an answer for 1 and 2. I think I need to add a separate redirect rule for that. I have asked a separate question to find the htaccess rewrite rule for that: How to redirect Mediawiki Short URL with no title to Main Page?
I will update this topic once I have the answer for those..
UPDATE:
For no: 1, I just need to move the rewrite rule to the top before other rewrite rules and it worked after that. More info: How to redirect Mediawiki Short URL with no title to Main Page?
For no: 2: wiki/index.php will not be redirected to wiki/Main_Page since it will look at index.php as a new article. It will only redirect w/index.php to /wiki/Main_Page
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I didn't want to bloat one post here with a ton of .htaccess questions, so I'm breaking them up individually. This one has to do with using RedirectMatch to create rules for existing, indexed pages that are part of an existing website.
Specifically, my test site has been up for about a year and I am now using .htaccess to re-write clean URLs for better SEO. So when I had a container page whose content was called based on a query string in a link, like so:
http://www.website.com/departments.php?dep=contact_us
I'm working on making the URL appear smoother like:
http://www.website.com/contact-us/
The tutorial I read suggested that changing my links to the new, snazzier SEO-friendly style would lead to lots of 404 errors and broken page links until the site has been indexed again and the records brought up to date.
Thus, my question: Is this a legitimate concern or will the new links be picked up in 24-48 hours?
I ask because I have several websites with many layers of file structure and I'm thinking that I'd need a TON of rules in my .htaccess file to fix all the broken links I may be creating.
If I'm creating confusion here, please let me know and I'll clarify things.
Many thanks in advance!
Thus, my question: Is this a legitimate concern or will the new links be picked up in 24-48 hours?
You can make it happen sooner if you externally redirect the client using a 301 (permanent). This is a lot trickier than internally rewriting on the server's end because you'll cause conflicting rules. For example, if you wanted to rewrite url-a to url-b internally on the server, then redirect the browser from url-b to url-a (meaning when a client specifically requests url-b to redirect it to url-a):
# internally rewrite url-a (fake URL) to url-b (where the actual content is)
RewriteRule ^url-a$ /url-b [L]
# externally redirect url-b (actual content) to url-a (fake URL)
RewriteRule ^url-b$ /url-a [L,R=301]
Since the rewrite engine loops, these 2 rules will continue to rewrite each other and cause a 500 internal server error (the same thing happens if you replace the second rule with a RedirectMatch). To fix this, you need to create a condition so that the external redirect (second rule) only gets applied if the actual request was for "url-b". You can do this by matching against the %{THE_REQUEST} variable, which is essentially the first line of an HTTP request.
Using your example URLs, you'd have something like this:
RewriteRule ^contact-us/?$ /departments.php?dep=contact_us [L]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /departments\.php\?dep=contact_us
RewriteRule ^ /contact-us/ [L,R=301]
This means when someone like a google-bot attempts to resolve http://www.website.com/departments.php?dep=contact_us, the 2nd rule's condition will match the request (which will look something like: GET /departments.php?dep=contact_us HTTP/1.1) and it will get redirected to http://www.website.com/contact-us/. At which point, the client (google-bot) will request /contact-us/ and the first rule will get applied and internally rewrite the URI to /departments.php?dep=contact_us.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Firstly, I viewed mod_rewrite - how to redirect a mistyped 'pretty' url however the OP is wanting his mistyped URL's to redirect to the correct, I simply want mine to error404.
quick question. I am looking for a solution to improve upon some mod re_write code I have.
Currently if someone was to enter an URL on my site that was a product page I.e. "/products/[category]/[product-name]" the .htaccess file has the following code
RewriteRule ^products\/([^/]+)\/([^/]+)$ ?page=product&product=$2 [L]
Which works exactly as intended however if someone mistyped only the [product-name] the template page still loads using that product-name in the URL as a variable and not landing a 404 error.
I handle all error pages like thus:
#ErrorDocument 404 /index.php?page=error404
Hoping someone can shine a light on how to adapt the former rewrite rule so pages that don't exist I.e. if we had a product called kettle and someone typed in ketle at the end of the URL it would 404 rather than still load the template page with ketle as the GET var.
Thanks in advance.
I'm assuming that you're index.php script handles the "page" and "product" parameters. It's going to be the only one that's going to know whether the "product" parameter actually points to a valid product name or not. If not, then you need to use the header function to generate the 404, and then display the contents of page=error404.
The htaccess file and mod_rewrite isn't going to know whether a page name is correct or mistyped, only the script that does the lookup will know.
You can even write a PHP script into the <head> element of your dynamic page, with this format as an example:
$con = mysqli_connect("$hostname", "$username", "$password", "$database");
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT content FROM pages WHERE id = $value LIMIT 1");
if (0 = mysqli_fetch_array($result)) {
echo '<meta http-equiv="location" content="URL=http://www.yourdomain.com/index.php?page=error404">';
}
Note: double check the code above if it's right, for it's just an example, and didn't reviewed well the syntax and if mysqli_fetch_array() is used to count the row(s) on the $result of the SQL queries.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a problem with a url a .htaccess.
I need that all url like : http://mywebsite.com/api/something go to my api/index.php cause I need to write a routing system from this place.
The .htacces must be inside the api/ folder.
What I'm trying to do here ,is a routing system who includes differents content inside the api/index.page depending on the url used.
For exemple if I go to http://mywebsite.com/api/activity I want to include a specific file without changing the url.
Do you think it's possible ? Have you an idea of the .htaccess content ?
Thanks for your help.
I've finally found the solution by myself so I post the solution, that may help another person later :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (.*) index.php [PT,QSA]
</IfModule>