I'm currently using this htaccess code in Apache to change the URL in address bar from http://www.domain.com/list?m=100 to http://www.domain.com/list/100
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]*)$ index.php?m=$1 [QSA,L]
</IfModule>
The above htaccess is on /list directory.
I have tried to convert it to nginx (using http://winginx.com/en/htaccess), but I couldn't make it work. This is what I tried
location /list/ {
if (!-e $request_filename){
rewrite ^/([^.]*)$ /index.php?m=$1 break;
}
}
The above just downloads the PHP code. I also tried changing the break to last, but it just opens the homepage (the url in the address bar changes to http://www.domain.com/list/100). Any suggestions how I can make it work?
You don't need to convert everything, use this:
location /list/ {
rewrite ^/list/(.*)$ /index.php?m=$1 last;
}
Don't forget to restart nginx afterwards to see the changes.
Related
I have a Symfony 4 web app. Let's assume it is http://example.com.
I have installed HESK in /public/hesk/.
Now HESK is accessible via http://example.com/hesk/index.php, but when user skips "index.php", 404 error is shown.
I have tried return $this->redirect('...') and return new RedirectResponde('...') in my controller, but browser only says that there are too many redirects.
What is the correct way to redirect /hesk and /hesk/ to /hesk/index.php?
If you use Apache, you can put a .htaccess file in /public/hesk/ to force the redirect, something along those lines :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php$1 [L,QSA]
</IfModule>
Or use the DirectoryIndex index.php directive in your virtual host directly.
For Nginx, you need to modify your vhost to add the redirect. Something like that (not tested) :
location /hesk {
try_files $uri $uri/ /index.php$uri&$args;
}
I'm working on a project where at a place I want to change my normal website address to ip address ie address http:/www.mydomain.com/index.php will change into http:/192.xx.xxx.25/. This will be only for one specific page not for whole site.
Can anyone help me how i will be able to do this. Thanks....
If you are using Apache you can do it using htaccess.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
//301 Redirect Old File
Redirect 301 /specific_page_slug http://192.168.0.1
For Nginx you can add following in your Nginx configuration file
location Old {
rewrite ^(.*)$ File redirect;
}
location /specific_page_slug {
rewrite ^(.*)$ http://192.168.0.1 redirect;
}
location / {
if ($http_host ~ "^example.com"){
rewrite ^(.*)$ http://www.example.com/$1 redirect;
}
}
The problem description is very specific, but i have no idea about its source
The project uses Laravel 5.1
I have a project copy on my computer and remote server. Local server is configured to remove /public from URL with VirtualHost. On remote server, project is located in subdirectory. E.g. /project. To get rid of public, there is an alias in httpd.conf and line RewriteBase /project in /project/public/.htaccess
The problem appears when using Laravel's pagination mechanism:
There are links to pages in the page with navigation. On local machine everything is OK, but on remote server clicking on link with URL like http://<server_ip>/project/navigationPage?page=N causes redirect to http://<server_ip>/navigationPage?page=N. Of course, 404 error appears.
What is the problem and how to fix it?
Full .htaccess in /project/public:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /project
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Ok, problem was in slash that was added to links by paginator. Link was looking like http:///project/navigationPage/?page=N, but must look like http:///project/navigationPage?page=N (without slash). Bug fixed by editing AbstractPaginator's url function:
public function url($page)
{
if ($page <= 0) {
$page = 1;
}
$parameters = [$this->pageName => $page];
if (count($this->query) > 0) {
$parameters = array_merge($this->query, $parameters);
}
return rtrim($this->path, '/').'?'
.urldecode(http_build_query($parameters, null, '&'))
.$this->buildFragment();
}
So, I've this problem:
Base Website located at http://example.com/
Second Website located at http://example.com/web2/
People making various requests to the second website like
http://example.com/myWeb/pg1 and http://example.com/web2/pg2
Recently and due to some other issues I need to have a custom new path for the second website but also keep the first one working.
The ideia is to allow users to access the second website over the two following addresses:
http://example.com/web2/
http://example.com/alternative-url-web2/
The folder /web2/ actually exists on the server, but how can I simulate the folder /alternative-url-web2/ and "redirect" the requests to /web2/?
Please note I don't want the URL on the browser to change, this must be a "silent redirect". And I also make sure that all other requests like http://example.com/other are not redirected by the second website.
Thank you.
Update:
According to #anubhava I could simply solve this issue by adding in my .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
This is probably working fine but I noticed the following:
http://ex.com/alternative-url-web2 is redirected to http://ex.com/web2/ (changing browser URL);
http://ex.com/alternative-url-web2/ is redirected to http://ex.com/(changing browser URL);
http://ex.com/alternative-url-web2/someRequest works fine and does NOT change the browser URL;
http://ex.com/alternative-url-web2/index.php works fine and does NOT change the browser URL;
Site Note:
At /web2/ there's an .htaccess that might be cause the wired redirect behavior above... So here is the file contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
Can the internal RewriteRule to index.php be causing all this? If yes, how can I fix it?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
Alternate code:
RewriteRule ^alternative-url-web2/?$ /web2/ [L,NC]
RewriteRule ^alternative-url-web2/(.+)$ /web2/$1 [L,NC]
This is a pretty simple rewrite. In the htaccess file in your document root, just add the following:
RewriteEngine On
RewriteRule ^alternative-url-web2/?(.*)$ /web2/$1 [L]
Unlike a redirect, which makes the browser/client send a new request for a new URL (thus changing what's in the browser's location bar), a rewrite happens entirely on the server's side.
By the way, in order to follow the trail of htaccess redirects, you could add something like this to each of them:
Header add X-Remark-Rewrite "/path.to/htaccess"
You can inspect these in the response in the developer tools.
i'm creating a website, before uploading the site to web hosting i'm using XAMPP on windows. i was trying to use Search Engine Friendly URLs, for example:
http://localhost/mysite/[something]
to
http://localhost/mysite/index.php?p=[something]
i tried this .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9A-Za-z]+) index.php?id=$1 [QSA,L] #and also tried http://localhost/mysite/index.php?p=$1
before this i tried more examples but they didn't work. The real problem is that for example if y type
localhost/mysite/some_page
i get an error (403), using the [R] flag i realized that redirection is: http://localhost/C:/XAMPP/htdocs/mysite/index.php?p=some_page
i removed htaccess file and restarted apache but problam persist with some urls
Perhaps I'm trying to solve a different problem, but if the [something] always leads to content served by your index.php, then just send all requests to index.php and let that page use the URL string to get the variable you need to serve the right content (ie the string after the /).
You may need to turn off MultiViews in your .htaccess using Options -MultiViews.
The following is what you need in your .htaccess
RewriteBase /
# Where "/" is the location of your index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA, L]
Hope this helps.