Url with trailing slash is redirected back to the host url - shopware6

i have sales channel with the domain "http://tld.de/staging" and only this domain, the only other sales channel is the default headless sales channel.
I created a custom route like that:
#Route("/path", name="frontend.path.index", options={"seo"="false"}, methods={"GET"})
And it works just fine when using the url http://tld.de/staging/path but when I use the url http://tld.de/staging/path/ (with trailing slash) it redirects me to http://tld.de/path
I already tried to add a second route for that method but that didn't work
#Route("/path/", name="frontend.path.index.trailing_slash", options={"seo"="false"}, methods={"GET"})
Did I miss something or is that just the default behaviour?
Additional information: The custom controller class extends the StorefrontController and there are not redirects happenening inside of the custom controller, I even added a dd() at the beginning of the method for testing. And If i add the trailing slash to the first route the url with the trailing slash works but the one without gets redirected instead.

The redirect of the url with the trailing slash to the one without is the default behavior of Symfony. If that redirect doesn't respect your base path, then it's probably an issue with your configuration.
However if you absolutely must serve both URLs without redirecting, then you could use wildcards to do so. Obviously this is unconventional but should work:
/**
* #Route("/path{trailingSlash}", name="frontend.path.index", methods={"GET"}, defaults={"trailingSlash"="/"}, requirements={"trailingSlash"="[/]{0,1}"})
*/

Related

htaccess redirect url with anchor tag

I am trying to redirect a URL to a page with an anchor tag. The redirect works but how do I keep the original URL after the redirect rather than the redirected one.
I want to redirect www.example.com/caves/ to www.example.com/trips.html#caves.
When I redirect I get the URL www.example.com/trips.html#caves but want it to keep www.example.com/caves/.
I have spent ages looking for answers but no luck, any help would be appreciated
Here is my code in htaccess
RewriteEngine on
RewriteRule ^Caves/(.*) /trips.html#Caves [NE,L,R]
I doubt it can be done. Anchor is a client side directive and you can't "hide" it with server side rewrite (the browser can't jump to anchor if it doesn't see one), so the anchor has to be in address bar URL.
...how do I keep the original URL after the redirect rather than the redirected one.
You want an internal rewrite, as opposed to an external redirect - which is what you are currently doing with the use of the R (redirect) flag on the RewriteRule.
RewriteRule ^Caves/(.*) /trips.html#Caves [NE,L,R]
Ordinarily you could simply remove the R flag to keep the original URL in the address bar. The request is internally rewritten to /trips.html.
However, the fragment identifier (ie. #Caves - or strictly everything after the #) is lost. The request is rewritten to /trips.html, not /trips.html#Caves. The fragment identifier is evaluated by the client, not the server and is no doubt being used by your client-side JavaScript to display the appropriate content. Because of the internal rewrite, the client-side JavaScript never gets to see the fragment identifier.
However, if you are internally rewriting the request (ie. /Caves/ is still present in the address bar) then your client-side code doesn't need the #Caves fragment identifier. It can simply look at the original URL, ie. /Caves/ instead. But this will require a small change to your client-side code.
(Aside... Note that you are using Caves (capital first letter) in your code, but referring to caves (all lowercase) in your description. The directive you have written is case-sensitive, so it should be /Caves/, not /caves/ in your description?)

How to hide part of a URL from clients using htaccess?

I'm working on a website running on an Apache server. The PHP factory makes and takes URLs that look like this:
site.com/page/view/[id]/[vanity-url]
However, the client wants to hide part of the URL from view, so in the browser it appears as:
site.com/[vanity-url]
The server still needs to see the full URL.
I've tried various RewriteRules after hours of searching, and the closest I seem to have come is:
RewriteRule ^page/view/(.*)/(.*)$ /$2 [R,L]
...but that doesn't seem to be doing anything. I know for sure that my .htacces is working.
What am I doing wrong?
Unfortunately, this isn't possible. Let me explain why:
You state that the server needs to see the full URL. Specifically, this includes the id segment, which I am sure your framework needs (it wouln't need it if the server only needs to see the vanity segment). The code you have provided redirects the full URL to the vanity URL, thus making the full URL invisible to the server for the next request.
So, navigating to /page/view/2/about-us would redirect to /about-us, and the address bar would change to reflect that. This causes a new request to be sent to the server, containing only the /about-us vanity URL.
As a result, you would need to rewrite the vanity URL back to the full URL (without redirecting, so that the /about-us stays in the address bar as-is), but you wouldn't be able to do this, as the vanity URL does not contain the id segment, which seems to be a requirement for the framework to serve the correct response. Keep in mind that Apache cannot guess the ID for that particular vanity URL.

redirect a subdomain to a remote url, preferably via only DNS setting or with httpd.conf, without changing url displayed

For example I wish to redirect list.mydomain.com to http://my.emailingapp.com/lists/
but keeping the name displayed in URL as "list.mydomain.com".
Note that all parameters are to be passed over. e.g. list.mydomain.com/?stuff=a should be the same with http://my.emailingapp.com/lists/?stuff=a
Another note: these domains are on different server.
There are many other similar posts, but all of them does not work exactly as I wanted to.
Adding a CNAME record for list.mydomain.com to my.emailingapp.com using which you can achieve the following.
URL remains list.mydomain.com
The arguments get displayed in URL as list.mydomain.com/?stuff=a
Your requirement of having /lists/ should be implementable by URL rewrite rules.

conditional redirect in routes

I want to do a conditional redirect in my routes file.
Users can opt to have a shorter url for their site. When this short url is available but the longer url is used, I always want to redirect them to the shorter version. For example: user goes fubar.mysite.com but fubarsite.com is available -> redirect from fubar.mysite.com to fubarsite.com .
I store the available shorter url in the database.
I want to do this in routes but don't know if this is possible. I tried the constraint approach but have no idea how to do the redirect in the block
constraints(Redirect) do
#need to redirect here
end
I think you should not do this in your routes file because it involves a lot of logic. Maybe you want to setup a before_filter in your application controller and do the check there. If the shorter url is available then do the redirect. You'll need a sort of catch all route in your routes file for doing that.

rewrite url without change address

I want to show a page instead of another page, without change the second page url.
I know this is possible with htaccess.
I copied that code from a cms htaccess:
RewriteRule ^event-([0-9]+)\.html$ calendar.php?action=event&eid=$1 [L,QSA]
with that code, we will redirect to calendar.php?......... but I want it redirect (without changing the address in address bar) to another site, for example to http://www.google.com/page......
Is it possible?
Thanks ..
If you want to provide content from another site without changing the address in the address bar of the browser that mean you becomes a proxy.
So check apache documention for proxy configuration (this can be done for specific urls only). Even mod_rewrite can do the proxy things with the [P] tag, mod_rewrite will allow a lot more 'specific url' filtering.
Now the job of a proxy, when he have the response from the distant website and he needs to render it for the HTTP client, is only to change the HTTP headers in the response. So only url in Location tags or such specific headers will be altered. You must known that all the HTML content from the distant website will not be altered (the inner links will be on www.google.com and not on your www.whythehelldoiproxygooglewithmysite.com).
If you want to alter this returned content check mod_proxy_html module, this will add some extra stuff before sending the resonse, to do some more reverse proxy alterations.